I have a struct defined globally in global.h as follows:
Code:
typedef struct entry {
char *lexeme; // id name
struct entry *next; // pointer to next entry
} ENTRY;
Also in global.h, I have:
Code:
ENTRY *symPtr=NULL;
Now in my main file, I have a fn that creates a node and adds it to a linked list:
Code:
ENTRY *addEntry(char *str)
{
ENTRY *pTmp=symPtr;
ENTRY *new;
new=(ENTRY *)malloc(sizeof(ENTRY));
if(!new) {
fprintf(stderr, "Could not add a new entry to symbol table\n");
exit(1);
}
// Set default values.
new->lexeme=str;
if(!pTmp) {
symPtr=new;
fprintf(stderr, "added %s\n", str);
}
else {
while(pTmp->next)
pTmp=pTmp->next;
pTmp->next=new;
fprintf(stderr, "%s added\n",str);
}
return new;
}
My problem is that symPtr always is pointing to null upon entry to this fn since when I tried to print out the linked list starting from symPtr, it only prints the last node that was added. Can anybody help? Thanks...