Jan 27, 2013 at 6:37am UTC
hi all, I have a function that append node to a linked list like this:
struct ListNode{
int value;
struct ListNode* next;
};
void appendNode(struct ListNode* head, int num){
struct ListNode* nodePtr;
struct ListNode* newNode;
newNode = malloc(sizeof(struct ListNode));
newNode->value = num;
newNode->next = NULL;
if(!head)
head = newNode;
else{
nodePtr = head;
while(nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
int main(){
struct ListNode* head;
head = NULL;
appendNode(head, 5);
printf("%p", head);
return 0;
}
when I use it, the head in main() does not change its address and it's still pointing to NULL. Why??
Jan 27, 2013 at 7:17am UTC
oh please, its not THAT hard to read without the code tags
Jan 27, 2013 at 7:26am UTC
@gtm its not, it is however common practice and decency, to make it easier for someone from whom you want help and in addition I do believe I gave the OP a resource from which they will more than likely get their answer.
Jan 27, 2013 at 7:27am UTC
> the head in main() does not change its address value and it's still pointing to NULL. Why??
The function is modifying a copy of the pointer.