I'm trying to set up my first linked list. I think I set it up right but when I try to print the contents to stderr I don't see what's expected. Can someone look at this code snipet and let me know if I'm reading the list correctly or if the problem is in how I write to the list?
typedef struct Nameval Nameval;
struct Nameval {
char *name;
int *age;
Nameval *next;
};
in main.....
{
char dbname[20];
int dbage;
Nameval *namelist, *listptr;
read from database......
if(dbage == something)
namelist = addname(namelist, newnameval(dbname));
for(; namelist->next != NULL; namelist = namelist->next)
fprintf(stderr,"name = %.20s\n",namelist->name);
}
Nameval *newname(char *name)
{
Nameval *newnm;
newnm = (Nameval *) malloc(sizeof(Nameval));
newnm ->name = name;
newnm->age = 20;
newnm->next = NULL;
return newnm;
}
Nameval *addname(Nameval *listptr, Nameval *newname)
{
newname->next = listptr;
return newname;
}
When I try to print to stderr, the same name constantly prints out and it's not one that I would expect to see. Thx.
}