&ptr is the same as ptr is a local variable inside test() . Since you call test() five times in a row without any intervention, it just gets the same address on the stack every time it is called (note: this is not required by C - this is just how your machine does it and as it usually happens).
If you call the second function, which itself is called test() , you wonβt get the same result for &ptr , since this is the space on the stack that ptr used to be in, is now using an intermediate function call.
For instance:
#include <stdio.h> void test(int *ptr) { printf("&ptr: %p\n", (void *) &ptr); printf("ptr: %p\n", (void *) ptr); printf("*ptr: %d\n\n", *ptr); } void test_test(void) { int a = 1; test(&a); } int main() { int a = 1, b = 2, c = 3, d = 4, e = 5; test(&a); test(&b); test(&c); test(&d); test(&e); test_test(); return 0; }
gives:
paul@local :~/src/c/scratch$ ./ptrtest &ptr: 0x7fff39f79068 ptr: 0x7fff39f7909c *ptr: 1 &ptr: 0x7fff39f79068 ptr: 0x7fff39f79098 *ptr: 2 &ptr: 0x7fff39f79068 ptr: 0x7fff39f79094 *ptr: 3 &ptr: 0x7fff39f79068 ptr: 0x7fff39f79090 *ptr: 4 &ptr: 0x7fff39f79068 ptr: 0x7fff39f7908c *ptr: 5 &ptr: 0x7fff39f79048 ptr: 0x7fff39f7906c *ptr: 1 paul@local :~/src/c/scratch$
and you can see that &ptr is different from the last call made via test_test() .
source share