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
?