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