"bitshadow" <ca********@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
It is a bit hard to know for sure without the actual sample code but it
looks like you are passing just a pointer to the list to the add and delete
functions. You need to pass the address of the list pointer in order to
update it. My test program was compiled with MSVC++ 2002 as an ANSI C
Program and runs as expected. I modified the add function to add another
node for each call.
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
struct list *next;
char data[20];
} list;
void add(list **phead);
void delete(list **phead);
void printlist(list *head, char *title);
int main()
{
int n;
list *head = NULL;
printlist(head, "At start:");
/* Add 3 nodes */
for (n = 1; n <= 3; n++)
{
add(&head);
printlist(head, "After add:");
}
delete(&head);
printlist(head, "After delete:");
return 0;
}
void add(list **phead)
{
list *head = *phead;
list* newnode = calloc(1, sizeof(list));
if (head == NULL)
{
sprintf(newnode->data, "1: head node");
*phead = newnode; /* will update ptr outside function */
}
else
{
/* Add another node at end of list */
int n;
for (n = 1; head->next; n++)
head = head->next;
sprintf(newnode->data, "%d: another node", n + 1);
head->next = newnode;
}
}
void delete(list **phead)
{
list *temp;
list *head = *phead;
while (head)
{
temp = head->next;
free(head);
head=temp;
}
*phead = NULL; /* updates ptr */
}
void printlist(list *head, char *info)
{
printf("%s%s\n", info, head ? "" : " <empty>");
for (; head; head = head->next)
printf("%s\n", head->data);
}
/* Expected output:
At start: <empty>
After add:
1: head node
After add:
1: head node
2: another node
After add:
1: head node
2: another node
3: another node
After delete: <empty>
*/
Hope this helps.
Regards,
Dag