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.
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.