#include <stdio.h>
int main(void) {
int a[4][2] = { {11, 12}, {21, 22}, {31, 32}, {41, 42} };
int *p = a[0];
printf("%d\n", *p);
printf("%d\n", p);
printf("%d\n", a[0]);
printf("%d\n", &p);
printf("%d\n", &a[0]);
printf("%d\n", a);
return 0;
}
Look at the above code.
In my understanding, since p
equals a[0]
, &p
and &a[0]
are supposed to have the same value.
But the actual output looks like this:
11
1772204208
1772204208
1772204200
1772204208
1772204208
Why are &p
and &a[0]
different?
What does &p
represent?
&p
yields the address of the pointer p
which obviously has to be different from the adress of a[0]
.
p
Is a pointer to an integer. It is stored in an address and it also stores an address.
Printing p
will print the address p
points to.
Printing *p
prints the value p
points to.
Printing &p
prints the address of p
, which is unique to p
and was allocated in the moment it was declared.
int *p_2 = &p;
p_2
will have the same value as &p
Every variable has its own unique address!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With