c Pointer to pointer, or passing list to functions
- by user361808
Hi,
I am new to c programming. Could anyone please tell me what's wrong with
the following program?
typedef struct Person_s
{
int age;
char name[40];
} Person_t;
int process_list(int *countReturned, Person_t **p_list)
{
Person_t *rowPtr=0;
//the actual program will fethc data from DB
int count =1;
if(!((*p_list) = (Person_t *) malloc(sizeof(Person_t))))
{
return -1;
}
rowPtr = *p_list;
rowPtr[count-1].age =19;
strcpy(rowPtr[count-1].name,"Prince Dastan");
*countReturned = count;
return 0;
}
int main(int argc, char *argv[])
{
Person_t *tmpPerson=0;
Person_t **p_list=0;
int *count=0;
int i;
process_list(count,p_list);
tmpPerson = *p_list;
for(i=0; i< *count; i++)
{
printf("Name: %s , age: %d\n",tmpPerson->name,tmpPerson->age);
tmpPerson++;
}
//free(tmpPerson);
return 0;
}