Luke Murray Luke Murray - 1 month ago 5
C Question

C pointer always contains its own memory address?

Why does the pointer p always point to its own memory address as an integer in the following example. I can't see where it is initialized and would guess that it would be a garbage value. Can someone show me why it is not a garbage value. By the way I am compiling this in gcc with -std set to c99.

#include <stdio.h>

int main() {

int *p; int a = 4;
p = &a;
printf("%d %u\n", *p, p);


Your problem (as the other answers point out) is with *p++;. What that says to do is increment the address in p then dereference it. From what you are seeing, we can assume p comes directly after a in memory

|something |  a  |  p  | something else |

So what ends up happening is p points to a, then is incremented so it points to itself (or more specifically: p stores the address that p is at).