0

In the following code p is pointer to an int. It is quite clear that p points to the address of i. Through my research i know &p points to the address of pointer p. But i don't get why would you need separate address for that. And also when would you use &p.

int main() {
    int i = 3, *p = &i;
    printf("%p",&p);
    printf("%p",p);
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
daft300punk
  • 169
  • 3
  • 16
  • 4
    `why would you need separate address for that?` - Variable `p` resides in memory, so it has an address (by the definition of "something that resides in memory"). – barak manos Jan 21 '15 at 14:11

3 Answers3

3

If p is pointer to int then

int **q = &p;

When you want to use pointer to pointer, then use the address of a single pointer to assign it to pointer to pointer.

Just to make a point that pointer is also a data-type and it stored in the memory location and it holds a valid memory location as its value. The address in which this valid memory location is stored is given by &p

Your printf() also needs to be fixed. %p expects void *

printf("%p",(void *)p);
haccks
  • 104,019
  • 25
  • 176
  • 264
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 1
    @CashCow This link has many good answers which says its good to have (void *) while printing pointers http://stackoverflow.com/questions/9053658/correct-format-specifier-to-print-pointer-address Standard says `p The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx). it shoud be pointer to void.` – Gopi Jan 21 '15 at 14:20
  • 1
    Do not use the term "double pointer" in this case. It does not mean *pointer to pointer*. It means "`double` pointer". – haccks Jan 21 '15 at 14:26
2

A trivial example:

int nochange(int *c, int *val)
{
    c = val; // Changes local pointer c to point to val
             // Note that C passes copies of the arguments, not actual references.
}
int do_change(int **c, int *val)
{
    *c = val; // Accesses the real pointer c at its real location and makes
              // that one point to val
              // Even though c is a pointer-to-pointer copy, its value is 
              // copied too, and the value is the address of the real c
}

int main()
{
    int a = 1;
    int b = 2;
    int *c = &a; // A pointer is also a datatype that resides in memory

    printf("%d\n", *c); // Will print 1
    nochange(c, &b);
    printf("%d\n", *c); // Will print 1
    do_change(&c, &b);
    printf("%d\n", *c); // Will print 2 because c now points to b
}

I have a similar answer with a bit more detail here about pointer vs pointer-to-pointer: pointer of a pointer in linked list append

Community
  • 1
  • 1
Jite
  • 4,250
  • 1
  • 17
  • 18
  • I think someone downvoted because this is not the question, I think the OP knows this. – Iharob Al Asimi Jan 21 '15 at 14:31
  • `and also when would you use &p`, i read that as a generic question of pointers. My answer gives a perfect example of when to use it. – Jite Jan 21 '15 at 14:33
  • I just assumed that if this was known to the author, he wouldn't have needed to ask the question in the first place. I might have missunderstood it though. – Jite Jan 21 '15 at 14:36
2

But i don't get why would you need separate address for that

You don't, but there exists the address of operator so you can take the address of a pointer, which is what

printf("%p\n", &p);

is printing.

And also when would you use &p

There are cases where this might be useful, consider for example that you need to pass a pointer to a function which could be reassigned into the function, you can do something like this

int allocateIntegerArray(int **pointerToPointer, size_t someSize)
{
    if (pointerToPointer == NULL)
        return 0;
    *pointerToPointer = malloc(someSize * sizeof(int));

    return (*pointerToPointer != NULL);
}

then you could use this funciton the following way

int *pointer;

if (allocateIntergerArray(&pointer, 10) == 0)
{
    fprintf(stderr, "Error, cannot allocate integer array\n");
    /* do some extra cleanup or recover from this error, or exit() */
    exit(0);
}

The pointers themselves are also variables and as such they need to be sotred somewhere, so the address of a pointer tells you where is the pointer stored, it's value tells you where it is pointing to.

By knowing where it is stored you can do things like the one explained above.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97