mr-woot
mr-woot

Reputation: 88

Print the addresses of a linked list

How the variables *head and temp values outputted.

#include <stdio.h>
#include <stdlib.h>

/* Link list node */
struct node {
    int data;
    struct node *next;
};

void pushList(struct node **head, int item)
{
    struct node *temp = (struct node *) malloc(sizeof (struct node));
    temp->data = item;
    temp->next = *head;
    *head = temp;

    printf("*temp = %ld\n"
           "temp->data = %d\n"
           "temp = %ld\n"
           "&temp = %ld\n", *temp, (temp)->data, temp, &temp);
    printf
        ("*head = %ld\n"
         "**head = %ld\n"
         "(*head)->next = %ld\n"
         "head = %ld\n"
         "&head = %ld\n", *head, **head, (*head)->next, head, &head);
}

int main()
{
    struct node *head = NULL;
    printf("&head = %ld\n", &head);
    pushList(&head, 1);
    printf("\n");
    pushList(&head, 2);
    return 0;
}

The output of the above is:

&head = 2686732
*temp = 1
temp->data = 0
temp = 1
&temp = 10292624
*head = 10292624 
**head = 1
(*head)->next = 0
head = 0
&head = 2686732

*temp = 2
temp->data = 10292624
temp = 2
&temp = 10292656
*head = 10292656
**head = 2
(*head)->next = 10292624 
head = 10292624 
&head = 2686732

Why is the value of *head equal to &temp?

Upvotes: 1

Views: 2055

Answers (2)

nemetroid
nemetroid

Reputation: 2159

You are passing *temp to printf, with the format specifier %ld, indicating long int. However, the type of *temp is not long int but struct node, which differs in size with long int. This means that printf's argument parsing logic gets messed up and you can't trust any of the output from that call to printf. For example, note how (temp)->data is showing as 0 and 10292624 instead of 1 and 2.

Further, you are using the wrong output specifiers for most fields (though the result might be correct depending on architecture, it is not portable). Try turning up the warnings level on your compiler (for gcc, -Wall will give you a bunch of warnings about this). You should optionally cast pointers to void* and use the %p specifier.

This (specifically, passing struct nodes to printf) is what makes *head and &temp come up as equal. Try changing your printfs to the following and they should make more sense:

printf("\n%d\t%p\t%p\n",(temp)->data,temp,&temp);
printf("\n%p\t%p\t%p\t\n\n%p\n\n\n",*head,
(*head)->next,head,&head);

Note that I removed the arguments *temp and **head since they refer to actual struct nodes, which printf can't handle.

Upvotes: 2

Douglas Su
Douglas Su

Reputation: 3364

In your code, The line *head = temp makes *head being always the same as temp.

Function pushList() always adds new element at the beginning of linked list, and head points to the first element of linked list all the time. So, it is obviously *head equals temp for the reason that temp points to the last allocated element which will be inserted at the beginning.

BTW, there is a better way to print the address of a variable that to use %p instead of %ld which will make your program more portable.

Upvotes: 1

Related Questions