C programming Pointers and Linked List Questions

JC0133

Senior member
Nov 2, 2010
201
1
76
I have a few questions that came up for me while I have been writing my program in my Operating Systems class. I got my program to work so I am not asking for help with my homework BUT I am really confused as to why some things I originally tried did not work. I good tons of examples online and nothing really answered my questions. Also I am very new to C programming. Most of my experience is in C++ and JAVA.

I was trying to delete the head of a linked list via a function that returns the deleted head.

EX: (I will make this a little generic but still to get my point across
struct nodeName *DeleteHead(Struct node *head) {
deleteHead = head;
head = head->next;

return deleteHead;
}//end of function DeleteHead

So I don't understand why this doesn't work. When I return to my main function. The head of the node has not be removed. Next thing I tried was to pass the actually address of the head function in the parameter with &head but this causes an error. To remove the head I would have to do head=head->next in the main function but deleteHead would return the head of the head->next not the originally head.

so I ended up doing something like this in the function above. It works in the function and in MAIN function but I don't understand why my other two ways didn't work above??? And I would expect this to work but I don't know why I need to do it.

struct nodeName *DeleteHead(Struct node *head) {
struct nodeName *deleteHead = malloc(sizeof(nodeName));
//placing head in deleted head address
*deleteHead = *head;

//used to move head to head->next
struct nodeName *temp = head->next;
head->data = temp->data;
head->next = temp->next;
free(temp);

return deleteHead;
}//end of function DeleteHead

1st. Question: Why does "Struct node *head" in the parameter of the function does not pass the pointer by reference? I thought all pointers where pass by reference by default?

2nd Question: Why does the original head = head->next in the function not work?

3rd Question: I don't understand why if I did deleteHead=head in the Function and I did head = head->next in the Main function(where it deleted the head), why did deleteHead move to the head->next. I wanted it to stay at the original head.

Like I said my program works, but I DO want to understand why it didn't work before as I like to learn and be more efficient.

Also I am looking for some other forums to join, that are similar to this one: where I can ask questions as well as read others post to learn from.

Thanks for your help.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
What you're doing here I'd call "pass reference by value". You passed a pointer, a reference, into the function, but all function arguments are passed by value unless otherwise specified. So you passed the address of the head node by value into the "head" variable. You can modify anything through the pointer and get side effects, but not the pointer address itself.

So how do you fix this? There's the old way or the new way. I think the new way works on most compilers now: Pass reference by reference:

Code:
struct node *DeleteHead(Struct node &*head)

And do what you originally did.

The old way is pass reference to reference by value:

Code:
struct node *DeleteHead(Struct node **head)

Then call that with a reference to the pointer. And always refer to head as (*head) inside the function.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,836
4,815
75
Turns out your right. Apparently C still doesn't support pass-by-reference.