1

I have read at least a dozen articles on %p now and I'm still not getting a clear answer on this. I realize there are multiple posts on this on the site and I read a good number of them. I even read this article, but it did not explain it adequately

Say I have:

int x = 10;
void *p = &x;

does the below statement print out the address of 'p' or the address of 'x'?

printf( "Here: %p\n" , p );

I am trying to get the address of x through p.


For some context, I am iterating up the stack looking for pointers pointing into a linked list I created:

void foo(){

    register void* base asm("ebp");
    void* iter = base;

    void* mBase = MAIN_BASE; //global defined as "register void* base asm("ebp")" in main function

    void* start = &head;
    void* fin = &tail + sizeof( tail );

    while( iter != mBase ){

        if( iter >= start && iter <= fin )
            fprintf( stdout, ">>>>%p\n", iter );

        printf("iter: %p\n", iter );
        iter = (void*)( (char*)iter + 1 );
    }
}
Community
  • 1
  • 1
Joshua
  • 4,270
  • 10
  • 42
  • 62
  • It prints the value of the variable "p", which is the address of the thing "p" points to. – Hot Licks Dec 02 '13 at 00:21
  • Is that right? Because in the example I provide, the print out of "iter" is always sequentially the next thing, shouldn't I be seeing some variations if it were returning the pointer's target? – Joshua Dec 02 '13 at 00:32
  • Who said anything about `p` having to be stored on the stack? Your compiler could be optimizing it out and only ever having `p` stored in registers. – tangrs Dec 02 '13 at 00:52
  • There are two examples, I'm talking about `iter`, not `p` – Joshua Dec 02 '13 at 00:55
  • The "pointer's target" is the storage location addressed by the value of the pointer. When you increment iter by 1 the printed address will increment by 1. (Of course, the "object" at that address may not be a valid object of any sort, and in fact the storage may not even be allocated.) – Hot Licks Dec 02 '13 at 03:47

4 Answers4

9

It prints the contents of variable p, which happens to be the address of x, so it prints the address of x. It displays it using the address format for a particular architecture. Format that may vary from one architecture to another: a variable stored in a far segment data, could be printed with a totally different format using a C compiler that produces MS DOS real mode executables, such as 025A:0008 or something similar.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
2

The statement prints the address of x, because this is the value stored in p. Of course p does not need to be valid, or to point to anything in particular.

In general, functions cannot take an address of a variable without you explicitly obtaining the address with the & operator. So if you wanted to print the address of p, you would have to do this:

 printf( "Here: %p\n" , (void*)&p );
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • But of course it's entirely possible that "p", in the general case, does not point at any existing object. – Hot Licks Dec 02 '13 at 00:22
1

With this kind of doubt, it usually helps me to printf both the address and the value of p and x. I've tried this code:

#include <stdio.h>

void main()
{
    int x = 10;
    void *p = &x;

    printf("value of x : %d\n", x);
    printf("value of x address in mem: %d\n", (int) &x);
    printf("value of p (integer): %d\n", (int)p);
    printf("value of p address in mem: %d\n", (int) &p);

}

And the output of this code is:

value of x : 10

value of x address in mem: -1079190472

value of p: -1079190472

value of p address in mem: -1079190468

You can see that the value stored in p is the address of x.

Trouble-lling
  • 333
  • 5
  • 15
0

It will print address of x because when we are saying

int *p=&x;

we are bascically doing these two steps:

int *p;
p=&x;

so p will store address of x.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Nikhil Pathania
  • 812
  • 6
  • 19