Pointers of Pointers

Hi All,

I am a bit of starter on C++ would appreciate some advise/guidance on pointers. I am working through the Jumping into C++ but one question has confused me slightly. I thought I would put the code into xcode but it doesn't work which I thought it wouldn't as the pointer cant be set as its an memory address, unless it had * (which is dereference). Any advise would be greatly appreciated.

4. What are the final values in x, p_int, and p_p_int in the following code:
int x = 0;
int *p_int = & x;
int **p_p_int = & p_int;
*p_int = 12;
**p_p_int = 25;
p_int = 12; <--- xcode doesnt like this
*p_p_int = 3; <--- xcode doesnt like this
p_p_int = 27 <--- xcode doesnt like this

answer
1
2
3
4
A.x = 0, p_p_int = 27, p_int = 12 
B.x = 25, p_p_int = 27, p_int = 12 
C.x = 25, p_p_int = 27, p_int = 3 
D.x = 3, p_p_int = 27, p_int = 12
Last edited on
It looks like something ate a '*' out of each of those three lines that xcode doesn't like, this is probably just a publishers error.

p_int = 12;
should be:
*p_int = 12;

since you are assigning a value to the address that the pointer contains, you are not assigning a value to the pointer itself. Similarly

*p_p_int = 3;
should read:
**p_p_int = 3;
and so on.

Think of this as a lesson OP, don't let English majors proof read your code.
Many thanks for your reply Computergeek01. That makes more sense. To clarify that a pointer address cant hold a value itself, but the question is alluding to that fact? So I have amended the code with the leading *. But the answer doesn't make any sense, as if any of the values change within the pointer structure because x is the root for all pointers then surely it can never be any of those answers above. They would all be the same value. This question seems like its completely wrong? But for a newbie its probably me whose wrong and doesn't understand.

1
2
3
4
5
6
7
8
9
10
   
 int x = 0;
 int *p_int = & x;
 int **p_p_int = & p_int;
    
     *p_int = 12;
**p_p_int = 25;
     *p_int = 12;
   **p_p_int = 3;
  **p_p_int = 27;
If you look at the answers, it will become apparent that missing was not the *, but the static_cast<int*/int**>, or, rather, question is abstract "what would happen if this was allowed"
I dont think thats covered if so then it should be clear from that question.

static_cast<int*/int**>
So, as I said question is abstract "what would happen if this was allowed"

If you look at the answers p_p_int is always 27 here and only 27 assigment is on the last line. (If you wonder, correct answer is C)

If you want, I can describe what happens step by step.
Last edited on
You are right it is C but totally confused, could you explain step by step be really appreciated.
The question shouldn't be abstract if your trying to learn pointers the correct way and dont know the rights and wrongs, in the learning phase. I guess.
Let's start:
int x = 0;
We are declaring variable x and assigning 0 to it. I will denote it as 
x(0)

int *p_int = & x;
Here we are declaring variable p_int of type pointer to int and assigning it a value of x address. In other words making it point to x 
p_int(&x), x(0) or (for clarity) p_int→x(0)

int **p_p_int = & p_int;
Here we are declaring variable p_p_int of type pinter to pointer to int and assigning it a value of p_int address. In other words making it point to p_int 
p_p_int→p_int→x(0)

*p_int = 12;
We assigning 12 to variable p_int points to: x.  
p_p_int→p_int→x(12)

**p_p_int = 25;
We assigning 25 to variable, variable pointed by p_p_int points to. p_p_int points to p_int and p_int points to x, so we are assigning 25 to x 
p_p_int→p_int→x(25)

p_int = 12;
We are assigning 12 to p_int. Now it not pointing to x address, but instead points to some arbitrary memory address 12. It would be a disaster if we were to dereference this pointer, but luckily we do not do that. 
p_p_int→p_int(12), x(25)

*p_p_int = 3;
We are assingning 3 to the variable p_p_int points to. p_p_int points to p_int, so we overwriting one arbitrary address with other. 
p_p_int→p_int(3), x(25)

p_p_int = 27
Now we simply making p_p_int to point to arbitrary address 27. 
p_p_int(27), p_int(3), x(25)
Last edited on
The best written explanation I've seen on pointers starts here: http://computer.howstuffworks.com/c20.htm

That starts from the basics, explaining why, what they are, how to use them and common errors.

This section is called Pointers to Pointers, should you wish to dive right in, but I recomend reading from the earlier link.
http://computer.howstuffworks.com/c32.htm
Thanks for the explanation MiiNiPaa your logic makes perfect sense and I too came to the same conclusion. But in reality and fact you cannot run that code and set the following values for a pointer arbitrary address, well at least not in xcode.

1
2
3
p_int = 12;
*p_p_int = 3;
p_p_int = 27


Thanks kbw will be sure to check those out.
you cannot run that code and set the following values for a pointer arbitrary address, well at least not in xcode.
Yes. Maybe it was done so you could not just run it and see results, maybe it is just an abstraction.

You can still see results with help of some casts:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    int x = 0;
    int *p_int = & x;
    int **p_p_int = & p_int;
    *p_int = 12;
    **p_p_int = 25;
    p_int = reinterpret_cast<int*>(12);
    *p_p_int = reinterpret_cast<int*>(3);
    p_p_int = reinterpret_cast<int**>(27);
    std::cout << "x       = " << x                                    << '\n' <<
                 "p_p_int = " << reinterpret_cast<uintptr_t>(p_p_int) << '\n' <<
                 "p_int   = " << reinterpret_cast<uintptr_t>(p_int)   << '\n';
}
x       = 25
p_p_int = 27
p_int   = 3
http://ideone.com/JFeYwC
Thanks for the help, but not come across casts yet hmm. Bit more studying to do. Did not realise you can use pointer to set a value in its address space which is the address of the pointed value.
Topic archived. No new replies allowed.