I read this: Two different values at the same memory address and this: How is a variable at the same address producing 2 different values? but both are about problems with const
qualifiers, so I think my question is different.
This is from a linked list implementation. head
is a struct Node*
, and walker
, used to print list elements etc..., is also a struct Node*
and is initialized as struct Node* walker = head
.
What's weird is that if I print out *head
with a %d
format (my struct Node
is a classic int value
and struct Node* next
format), I get 1
, which is what I set up the value of the first Node
to be.
However, when I print out *walker
using the same %d
format, I get some weird decimal value such as 1525189360
.
I check however that both head
and walker
hold the same value, which is the address of the first Node
, so I really don't understand that discrepency.
What's even weirder is that if I print out head->value
, I get 1
again, and if I print out walker->value
, I also get 1
this time!
Code
struct Node {
int value;
struct Node* next;
};
struct Node n1;
n1.value = 1;
struct Node* head;
head = &n1;
struct Node* walker = head;
printf("address of walker: %p, address of head: %p, value in walker: %p \
,value in head: %p, value pointed to by walker: %d, value pointed \
to by head: %d\n", &walker, &head, walker, head, *walker, *head);
Print Out:
address of walker: 0x7fff5ae88aa8, address of head: 0x7fff5ae88b00, \
value in walker: 0x7fff5ae88af0, value in head: 0x7fff5ae88af0, \
value pointed to by walker: 1525189360, value pointed to by head: 1