I have a list with a few nodes. I’m trying to mark the final node by declaring it’s current->next value as NULL.
So, let’s say that my current is pointing at the final node. Do the two following code snippets work in a different way, and if yes, how?
current->next = NULL;
and
current = current->next;
current = NULL;
I have been experimenting with these for a while now but I cannot really understand what is going on in there. I would say they are doing the same thing, as the second one first points my current to where next points and then sets it to NULL.
EDIT : Oh, I understand what the answers are saying, and it looks quite obvious to me too now :). So if I do not use the first way, but opt to go for current = current->next is there a way for me to put the NULL value where I ought to? How can I use my new current to point where my old next was pointing at?
The former snippet assigns the
nextfield of the struct pointed to bycurrenttoNULL. The second snippet makes thecurrentpointer point at thenextfield of the struct pointed to bycurrentand then setscurrenttoNULL.I don’t really understand the usefulness of the second unless you have some other way to get a reference to the struct, since you essentially cut yourself off from the struct by losing your pointer to it.
So in the former case:
In the latter case: