Thread: pointer error
View Single Post
Old 07-13-2006, 09:49 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Well... '%s' will assume the argument is a string, or more closely a char*, since you're passing a Node* which is a container class, you can't expect printf() to knwo, what to do with it..
Why not make it wee bit more simple like:
Code:
printf("\n node /t %s", temp->string);
Since you know your Node struct has a member called string, which is a char*.
Byt the way what good is this ??
Code:
Node *addhead(char *string, Node *temp)
{
    Node *new = newNode(string);
    Node *head = NULL;
    printf("\n node /t %s", temp);
    head = new;
    printf("\n List /t %s", new);	
     return (new);
}
Since you create the new container, which is pointing to a copy of the Node struct which has (or should have) initialy set it's member string to the value of argument string yet it is practicaly doing nothing...

Then you assign a new Node* called head with the same memory address.. yet when the function returnes it is only the new member which is returned, which effectively will kill your head container...
I'm suspecting you wanted something like:
Code:
Node *addhead(char *string, Node *temp)
{
    Node *new = newNode(string);
    temp->next = new;
    new->next = NULL;
    return (new);
}
For the printing of things I can't see what you're trying to do.. You create a form of linked list, yet only have a pointer to the next item, in order to print the entire list, you need to know teh initial list starting container, and have a dedicated printing function which would run through the list, given the starting point, printing whatever it encounters...
Perhaps something like:
Code:
int print_list(Node* start)
{
    Node* tmp=start;
    int i=0, j;
    while(tmp){
         printf("> %s\n", tmp->string);
         for(j=0; j < i; ++j)
             printf(" ");
         tmp = tmp->next;
         ++i;
    }
    return i;
}
Not only will this print every string container in your list, but it will also return how many items your list is holding.

But if you plan on doing some serius mengling with this, I would suggest somethign like a double linked list, so you can search forward or backwards through it, given teh required action.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote