B
bitshadow
I've been working on a link list implementation and I'm driving myself
crazy trying to understand something.
assuming i have a list and i have the following quasi pseudocode:
add(list *head):
if(head==NULL)
head=newnode
else
while (head)
head->data="something"
head=head->nextNode
I assumed that since head was a ptr whatever was done to in the
function would be reflected once this function terminated? is this true
or is head just a local var of a address? The reason i ask this is
because on my liist if head is null, on exit from the function head
still remains null even though i specfied it should get the new ptr.
HOWEVERRRRR....if head is not null, the program iterates through the
list and updates the list accordingly. If its only supposed to be a
copy how come the second part of the function retains the value after
the function terminates, and how come the first part doesn't. more
precisely how comes all the memory from head+1 and on is updated
seemingly by reference and head is not.
in fact if i write the follwoing:
delete(list *head):
while(head)
temp = head->next
free(head)
head=temp
on exit. everyone one of them is freed. EXCEPT head. obviously i'm not
freeing head but why? Please let me know if i've explained this
properly. i just feel like i'm going crazy trying to understand this.
thanks
crazy trying to understand something.
assuming i have a list and i have the following quasi pseudocode:
add(list *head):
if(head==NULL)
head=newnode
else
while (head)
head->data="something"
head=head->nextNode
I assumed that since head was a ptr whatever was done to in the
function would be reflected once this function terminated? is this true
or is head just a local var of a address? The reason i ask this is
because on my liist if head is null, on exit from the function head
still remains null even though i specfied it should get the new ptr.
HOWEVERRRRR....if head is not null, the program iterates through the
list and updates the list accordingly. If its only supposed to be a
copy how come the second part of the function retains the value after
the function terminates, and how come the first part doesn't. more
precisely how comes all the memory from head+1 and on is updated
seemingly by reference and head is not.
in fact if i write the follwoing:
delete(list *head):
while(head)
temp = head->next
free(head)
head=temp
on exit. everyone one of them is freed. EXCEPT head. obviously i'm not
freeing head but why? Please let me know if i've explained this
properly. i just feel like i'm going crazy trying to understand this.
thanks