0

I wrote this to test my knowledge on pointers:

int main (){
  int seven = 7;
  int* p = &seven;
  int** pp = &p;
  int*** ppp = &pp;
  printf("%d %d %d %d\n", *ppp, &pp, &p, &seven);
}

However, in the output, I get:

1363848032 1363848024 1363848032 1363848044

This is unintuitive to me because *ppp == &p

I was expecting *ppp == &pp. Why is this happening? Does the &pp follow the pointer to p?

2
  • 3
    always use the "%p" format specifier for printing pointer addresses! also, return something from main (as it has to return int), to be pedantic return EXIT_SUCCESS from stdlib.h, and last but not least, eather write int main(void) or int main(int argc, const char *argv[]) Commented Oct 6, 2015 at 21:42
  • 1
    Yessir, will do in the future @PeterVaro Commented Oct 6, 2015 at 21:43

4 Answers 4

3

I will try to explain the problem with the following schema:

  ppp     pp       p    seven      <---- variable names
+----+  +----+  +----+  +----+
| 30 |  | 20 |  | 10 |  |  7 |     <---- memory contents
+----+  +----+  +----+  +----+
  40      30      20      10       <---- memory addresses

In the above diagram the boxes represent individual memory slots where your variables are stored. The number under each one of those boxes is the address of that place in memory.

Now pointer really only is a variable whose value is a number that happens to be the memory address of some other variable. So when you say *ppp you ask for the value of variable that is at address 30, which equals to 20, which is the address of p. So you get *ppp == &p and ppp == &pp.

Sign up to request clarification or add additional context in comments.

Comments

1
int*** ppp = &pp;

So why do you expect *ppp == &pp? (note the extra * dereference)

7 Comments

Shouldn't ppp's value be the addr of pp? And therefore when dereferencing, *ppp == (addr of pp)?
You shoudn't dereference to get the value of something.... If ppp has a value, to get it, you write ppp....
@ylun.ca ppp == &pp, so *ppp == *(&pp)
Interesting.... So basically I should think of a pointer as a box that holds the address of the pointer it points to. So therefore when I dereference a pointer it retrieves the value in the box of what the pointer points to?
@KarolyHorvath So then when I dereference, I'm actually getting the value of pp, which is &p. (Right??)
|
1

There's an easy way of looking at this: Boxes and Box numbers. You can put all kinds of numbers into boxes, including box numbers.

In your example:

7 is put into a box (variable seven) which has a box number of &seven.

Then you create a new box (variable p) to hold 7's box number (&seven), which in turn has a box number of &p.

Later you do the same, and so on.

ppp holds pp's box number, which was created to hold p's box number which in turn was created to hold seven's box number.

*ppp = pp
*pp  = p
*p   = seven = 7  

Comments

1

Are you trying to do something like this ?

#include <stdio.h>

int main (){
  int seven = 7;
  int* p = &seven;
  int** pp = &p;
  int*** ppp = &pp;

  printf("%d %d \n", *p, seven);
  printf("%d %d \n", **pp, seven);
  printf("%d %d \n", ***ppp, seven);


  printf("%p %p \n", p, &seven);
  printf("%p %p \n", pp, &p);
  printf("%p %p \n", ppp, &pp);
  printf("%p %p \n", *ppp, pp);

}


        $ ./ptr 
        7 7 
        7 7 
        7 7 
       0x7ffe4dce54b4 0x7ffe4dce54b4 
       0x7ffe4dce54a8 0x7ffe4dce54a8 
       0x7ffe4dce54a0 0x7ffe4dce54a0 
       0x7ffe4dce54a8 0x7ffe4dce54a8 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.