Using the debugger, the linked list seems to be successfully created inside the function, but it doesn't get updated "outside" in main. I don't know why it isn't updating since I'm using addresses and dynamic memory allocation, which if I'm not mistaken, doesn't get "cleared" once the function is exited.
int populate(node* list)
{
node* temp = NULL;
while(1)
{
printf("insert word: ");
char* word = getstring();
if(strcmp(word, "stop") == 0)
{
break;
}
//create a node
node* n = malloc(sizeof(node));
if(n == NULL)
{
return 1;
}
//put stuff in node
n->word = word;
n->next = NULL;
if (list == NULL) //first case only
{
list = n;
temp = n;
}
else
{
//set previous next to current node
temp->next = n;
//set pointer to current node
temp = temp->next;
}
}
}
int main()
{
node* list = NULL;
while(1)
{
printf("insert command: ");
char* word = getstring();
if (strcmp(word, "stop") == 0)
{
break;
}
else if (strcmp(word, "add") == 0)
{
populate(list);
}
else if (strcmp(word, "read") == 0)
{
readList(list);
}
}
}
Also, after my code runs, is the memory I've allocated automatically freed? Or am I gobbling up small chunks of my computers memory every time I test my program. (I'm using Xcode)