Thread: need help with: pointer to pointer to an array of struct

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    60

    Question need help with: pointer to pointer to an array of struct

    Hey guys, I have a little code like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    
    #define  KH_SUCCESS				0x00
    #define EKH_MAX_PERSON_EXCEED	0x01
    
    struct _KPerson {
    	char *name; // person name
    	int age; // person age
    };
    typedef struct _KPerson KPerson;
    
    struct _KHome {
    	KPerson **plist; // pointer to pointer of struct array
    	int msize; // max size alloweed
    	int csize; // current size
    };
    typedef struct _KHome KHome;
    
    KPerson* kperson_new(const char* _n, const int _a)
    {
    	KPerson *kp;
    	kp = (KPerson *) calloc( 1, sizeof( KPerson ) );
    	
    	kp->name = (char *) calloc( strlen(_n) + 1, sizeof( char ) );
    	strncpy( kp-> name, _n, strlen(_n) + 1 );
    	
    	kp->age = _a;
    	
    	return kp;
    };
    
    KHome* khome_new( const int _ms )
    {
    	KHome *kh;
    	int i = 0;
    	kh = (KHome *) calloc( 1, sizeof( KHome ) );
    	
    
    	kh->plist = (KPerson **) calloc( _ms, sizeof( KPerson * ) );	
    	for( ; i < _ms; ++i )
    		kh->plist[i] = NULL;
    	/* ??? [1] - how to initialize all persons in list to NULL */
    	
    	kh->msize = _ms;
    	kh->csize = 0;
    	
    	return kh;
    };
    
    int khome_insert( KHome *kh, const KPerson* kp )
    {
    	if( kh->csize >= kh->msize ) 
    		return EKH_MAX_PERSON_EXCEED;
    
    	/* ??? [2] - How to insert a new person value into home person list */
    	return KH_SUCCESS;
    };
    
    int main( int argc, char **argv )
    {
    	/* testing code here */
    
    	return 0;
    }
    As you can see above, I inserted my 2 questions at place

    ??? [1] - how to initialize all persons in list to NULL
    ??? [2] - How to insert a new person value into home person list

    I wonder of how to access each element in this kind of pointer.
    Code:
    struct _KHome {
    	KPerson **plist; // pointer to pointer of struct array
    	int msize; // max size alloweed
    	int csize; // current size
    };
    KHome *kh;
    How to access each element in plist ?

    Note: i don't want to use linked list here, just raw dynamicaly allocated pointer style.
    Last edited by bvkim; 05-15-2009 at 06:43 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As for question 1, you put the question directly after the answer -- use a for-loop to assign each kh->plist[i] to be NULL. Probably, you don't have to do that, since calloc will fill each of your kh->plist[i] with all-bits-zero, which is usually the same deal.

    As to 2, you have to decide where to put the guy, then use malloc to give you a piece of memory, and assign the return value of malloc to kh->plist[appropriate_number].

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by tabstop View Post
    As for question 1, you put the question directly after the answer -- use a for-loop to assign each kh->plist[i] to be NULL. Probably, you don't have to do that, since calloc will fill each of your kh->plist[i] with all-bits-zero, which is usually the same deal.

    As to 2, you have to decide where to put the guy, then use malloc to give you a piece of memory, and assign the return value of malloc to kh->plist[appropriate_number].
    tks for helping me, however, this is what I implemented for question 2.

    I commented on the code below

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <string.h>
    
    #define  KH_SUCCESS				0x00
    #define EKH_MAX_PERSON_EXCEED	0x01
    
    struct _KPerson {
    	char *name; // person name
    	int age; // person age
    };
    typedef struct _KPerson KPerson;
    
    struct _KHome {
    	KPerson **plist; // pointer to pointer of struct array
    	int msize; // max size alloweed
    	int csize; // current size
    };
    typedef struct _KHome KHome;
    
    KPerson* kperson_new(const char* _n, const int _a)
    {
    	KPerson *kp;
    	kp = (KPerson *) calloc( 1, sizeof( KPerson ) );
    	
    	kp->name = (char *) calloc( strlen(_n) + 1, sizeof( char ) );
    	strncpy( kp-> name, _n, strlen(_n) + 1 );
    	
    	kp->age = _a;
    	
    	return kp;
    };
    
    KHome* khome_new( const int _ms )
    {
    	KHome *kh;
    	int i = 0;
    	kh = (KHome *) calloc( 1, sizeof( KHome ) );	
    
    	kh->plist = (KPerson **) calloc( _ms, sizeof( KPerson * ) );	
    	
    	kh->msize = _ms;
    	kh->csize = 0;
    	
    	return kh;
    };
    
    int khome_insert( KHome *kh, const KPerson* kp )
    {
    	if( kh->csize >= kh->msize ) 
    		return EKH_MAX_PERSON_EXCEED;
    
    	/* ??? [1] - What's wrong with this part of code? */
    
    	kh->plist[kh->csize]->name = (char *) calloc( strlen( kp->name ) + 1, sizeof( char ) );
    	strncpy( kh->plist[kh->csize]->name, kp->name, strlen( kp->name ) + 1 );
    	
    	/* ============================================== */
    	
    	kh->plist[kh->csize]->age = kp->age;
    	++kh->csize;
    	
    	return KH_SUCCESS;
    };
    
    int main( int argc, char **argv )
    {
    	/* testing code here */
    	KHome *kh = khome_new(3);
    	KPerson *kp = kperson_new( "Pete Houston", 22 );
    	int res = khome_insert(kh, kp); // segmentation fault
    	if( res == KH_SUCCESS ) 
    		printf(" %s \n ", kh->plist[0]->name);
    
    	free( kh );
    	return 0;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If kh->plist[kh->csize] is NULL, then trying to follow that pointer (by doing kh->plist[kh->csize]->name) is going to crash. kh->plist[kh->csize] needs to point to valid memory first, and in order to get that memory you have to ask for it (with malloc). Hint: You've already done this in the "new" function.

    Note that your calloc at the first only got you a bunch of pointers; it didn't give you any actual objects.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by tabstop View Post
    If kh->plist[kh->csize] is NULL, then trying to follow that pointer (by doing kh->plist[kh->csize]->name) is going to crash. kh->plist[kh->csize] needs to point to valid memory first, and in order to get that memory you have to ask for it (with malloc). Hint: You've already done this in the "new" function.

    Note that your calloc at the first only got you a bunch of pointers; it didn't give you any actual objects.
    i've already allocated before copying the content.
    segmentation fault !

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bvkim View Post
    i've already allocated before copying the content.
    segmentation fault !
    That's true. So then why aren't you using that pointer? Set the kh->whatever equal to the pointer to the allocated object and you're done. The kh->whatever pointer equals NULL, so you can't follow it or use it until you assign it a valid pointer.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    60
    Quote Originally Posted by tabstop View Post
    That's true. So then why aren't you using that pointer? Set the kh->whatever equal to the pointer to the allocated object and you're done. The kh->whatever pointer equals NULL, so you can't follow it or use it until you assign it a valid pointer.
    oh dear ^^!
    fixed !

    tks so much buddy !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM