-2

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;
    *p++;
    printf("%d %u\n", *p, p);
}
Luke Murray
  • 861
  • 8
  • 10
  • 2
    Your code contains undefined behavior. Trying to explain the output of the program is a pointless exercise. See [this](https://stackoverflow.com/a/33797630/3386109), [this](https://stackoverflow.com/questions/2397984), and [this](https://stackoverflow.com/questions/4176328) for more information. You can however, use a debugger to step through the assembly code. – user3386109 Sep 12 '16 at 18:46
  • thank you. I would accept your answer if I could. My question was really about why the undefined behavior seemed deterministic, but I'm sure the assembly code will help. – Luke Murray Sep 12 '16 at 18:48
  • Can you clarify? You can't see were it's initialised? What about "p = &a;"? – Steve Sep 12 '16 at 18:52
  • 1
    @Steve Note that in C, `p = &a;` is _assignment_. `int a = 4;` is _initialization_. `p` is not _initialized_. – chux - Reinstate Monica Sep 12 '16 at 21:18
  • @chuck: I can't believe I missed that. – Steve Sep 14 '16 at 12:36

2 Answers2

4

Your problem (as the other answers point out) is with *p++;. What that says to do is dereference p then increment the address in p.

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).

Riley
  • 698
  • 6
  • 11
0

First you need to print a pointer value with %p, and your code has undefined behavior. You move the pointer one place after a and dereference it.

Your code doesn't illustrate the point you (it seems) wanted, the following will:

#include <stdio.h>

int main() {

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

It produces something like:

4 0x7fff5c17da44 0x7fff5c17da48

p points to a then *p is the value of a. The value of p is 0x7fff5c17da44 which is the adresse of a and the address of p (&p) is 0x7fff5c17da48.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • could you try inserting the line *p++; after the line p=&a. I used %u to illustrate the fact that the memory addresses are the same, but you'll see that this causes the output to change. After incrementing the pointer, the pointer points to a memory address, which contains an integer representation of itself. My question is why that integer representation is in memory there, when it doesn't appear to ever have been assigned. – Luke Murray Sep 12 '16 at 18:37
  • I told you that it will produce an "undefined behavior": pointing just after an existing variable is authorized, but reading the value here is "undefined behavior". – Jean-Baptiste Yunès Sep 12 '16 at 18:39
  • No you didn't illustrate that the addresses are the same. – Jean-Baptiste Yunès Sep 12 '16 at 18:39