Hi,
I've been working my way through some examples about how to program C, and I think I'm beginning to understand the basics of linked lists. In the following tutorial, they use some temporary variables to work their way through a linked list.
C7
However, the full code isn't shown, and the link to the example code doesn't work. So I have a question.
Before this code, they must have declared and allocated memory for "current_node" first, right?
Code:
// First deal with the base case, setting the head pointer
head = (struct list_node *) malloc(sizeof(struct list_node));
head->node_id = 1;
// Set a temp variable to our current last node
current_node = head;
When I made a program based on the example, I did the following first:
Code:
struct person *current_node;
current_node = (struct person *) malloc(sizeof(struct person)); // allocate memory
Was I right to do this? It seems like I had to. Similarly, at the bottom of the tutorial, they do this:
Code:
// Copy the head pointer
temp = head;
Again, you would need to do something like this first, right?
Code:
struct person *temp;
temp = (struct person *) malloc(sizeof(struct person)); // allocate memory
Thanks.