1

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?

mr-woot
  • 88
  • 1
  • 10
  • '(*head)->next,head,&head);' - takes the adddress of a local parameter:( – Martin James Jul 04 '15 at 09:01
  • 1
    Use your debugger to trace execution. – Martin James Jul 04 '15 at 09:01
  • 1
    See [Correct format specifier to print pointer address?](http://stackoverflow.com/questions/9053658) The correct header to use is ``; the `` header _does_ declare `malloc()` et al, but also provides other facilities which you are not using (and which people who use `` instead of, or as well as, `` almost never use). – Jonathan Leffler Jul 04 '15 at 09:23
  • Thanks for this helpful knowledge – mr-woot Jul 04 '15 at 09:31

2 Answers2

2

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.

nemetroid
  • 2,100
  • 13
  • 20
1

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.

Douglas Su
  • 3,214
  • 7
  • 29
  • 58