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.