2

Lets say in my linked list class I am keeping a pointer that refers to the head of the linked list...

In one of the member functions when I write this code...

Node *cur = head;
cur = cur->next;

why doesn't head change head->next?

if I write cur = NULL; will it make head null?

3
  • 3
    Well, what is a variable? (Ignore reference parameters and all that jazz) Commented Jan 10, 2013 at 22:45
  • Why don't you try it and see what results you get? Commented Jan 10, 2013 at 22:46
  • It works the same as int x = 0; int y = x; y = 1;. Why doesn't the value of x change when we assign to y? Commented Jan 10, 2013 at 23:06

3 Answers 3

7

I'm assuming that head is a Node *. In this case, when you say cur = cur->next you are changing where cur points, but head will remain pointing to the head of the list because you haven't changed where it points.

Cur----------|
             |
             V       next
Head -----> Item 1--------->Item 2

cur = cur->next yields the following:

Cur--------------------------|
                             |
                     next    V      
Head -----> Item 1--------->Item 2
Sign up to request clarification or add additional context in comments.

Comments

2

If you write:

Node *cur = head;
cur = NULL;

then cur will be pointing to nothing and nothing will happen to head.

If you write:

Node *cur = head;
cur = cur->next;
cur = NULL;

The same thing will happen (cur will be pointing to nothing and nothing will happen to head) unless head was NULL, in which case, you'll crash on cur = cur->next;

If you're trying to set head->next to NULL via the cur pointer, you can do so with the following:

Node *cur = head;
cur->next = NULL;

I can see your confusion is coming from the nature of pointers. When you say Node *cur = head; you are creating a Node pointer that points to the same memory address as head. If you set cur to something else via cur = NULL, then you're only changing what cur points to, not the value of what it's pointing to and therefore, head is left alone.

When you use cur->next = NULL instead, you're modifying the next member of what cur points to. This is also the next member of what head points to and therefore, the change is also reflected in head->next.

7 Comments

cur.next = NULL; will this change head?
@MadanLal head and cur point to the same thing, any operations on either one will affect the other.
@MadanLal Kind of... you need to use -> because cur is a pointer so it would be cur->next = NULL.
@HunterMcMillen: No, that's not true. Any operations on the thing that cur points to will affect the thing head points to (until they point to different things). But operations on cur, such as cur = x;, will have no effect on head.
@HunterMcMillen You should probably clarify that to if you perform any operations on the contents of head or cur.
|
1

cur is pointer, cur = cur->next; merely make cur point somewhere else, why should it change head?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.