2

I am attaching below the code of head_insert to insert a new node at the head of a linked list. The function is called with head_insert(head).

I am not sure about the syntax of the first argument of the function, I was expecting NodePtr instead since it is already a pointer, see below.

Why the code uses NodePtr &head and not NodePtr head only as head is already a pointer?

void head_insert(NodePtr & head, int the_number)
{
  NodePtr temp_ptr;
  temp_ptr=new Node;
  temp_ptr->data=the_number;
  temp_ptr->link=head;
  head=temp_ptr;
}


struct Node
{
  int data;
  Node *link;
};

typedef Node* NodePtr;
7
  • why the code uses "NodePtr &head" and not "NodePtr head" only as head is already a pointer? Commented Feb 18, 2013 at 7:52
  • 1
    Don't tag questions with both [c] and [c++]. While C++ understands C code, what is considered good style in C is rarely good style in C++. Commented Feb 18, 2013 at 7:52
  • @saraMcKnight: That should have been an edit, not a comment. Commented Feb 18, 2013 at 7:53
  • but even if u pass per value, head will be changed as head holds an address of the head node Commented Feb 18, 2013 at 7:54
  • i already edit my question Commented Feb 18, 2013 at 7:55

2 Answers 2

4

why the code uses "NodePtr &head" and not "NodePtr head" only as head is already a pointer?

The reason is that it needs any changes that the function makes to head to be visible to the caller.

If head were passed by value (NodePtr head) rather than by reference (NodePtr& head) this wouldn't be the case: when the function would assign temp_ptr to head, this change wouldn't propagate back to the caller. Passing head by reference addresses this.

Sign up to request clarification or add additional context in comments.

1 Comment

but even if u pass per value, head will be changed as head holds an address of the head node
0

Your typedef is a little confusing at first glance. In C, you should pass a struct Node ** head argument and assign its value to the newly created node : *head = temp_ptr;. Here is what it would look like :

void head_insert(struct Node **head, int the number)
{
    struct Node *new_head = NULL;
    // Alloc memory for new_head, etc..
    new_head->data = the_number;
    new_head->link = *head;
    *head = new_head;
}

Thus, to make the assignment *head = new_head, you need to have a pointer to your list head which is also a pointer. Otherwise, the update performed in the function would remain local. In your code, you take a reference to a pointer, which has merely the same effect than passing a "double" pointer.

6 Comments

but if i used instead head_insert(struct Node *head,...)
If you did that, the head = new_head would not affect head outside the function.
It seems you are not aware of the stack behaviour. You should take a look at this SO question : stackoverflow.com/questions/79923/…
Briefly, the memory for the arguments of your function is on the stack, this makes them LOCAL to a function call. That said, if you modify the address pointed by your struct Node *head argument, it will modify the value on the corresponding stack area which is only related to the current function call. Consequently, when you exit the function, you lose the assignment. Hence the need for struct Node **head (or a C++ reference).
Thanks Rerito...i get it now. any good textbook your recommend for further reading
|

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.