Reputation: 7547
I was following this tutorial about how does a pointer to a pointer work.
Let me quote the relevant passage:
int i = 5, j = 6, k = 7; int *ip1 = &i, *ip2 = &j;
Now we can set
int **ipp = &ip1;
and
ipp
points toip1
which points toi
.*ipp
isip1
, and**ipp
isi
, or 5. We can illustrate the situation, with our familiar box-and-arrow notation, like this:
If then we say
*ipp = ip2;
we've changed the pointer pointed to by
ipp
(that is,ip1
) to contain a copy ofip2
, so that it (ip1
) now points atj
:
My question is: Why in the second picture, is ipp
still pointing to ip1
but not ip2
?
Upvotes: 142
Views: 13328
Reputation: 213842
My very personal opinion is that pictures with arrows pointing this way or that make pointers harder to understand. It does make them seem like some abstract, mysterious entities. They are not.
Like everything else in your computer, pointers are numbers. The name "pointer" is just a fancy way of saying "a variable containing an address".
Therefore, let me stir things around by explaining how a computer actually works.
We have an int
, it has the name i
and the value 5. This is stored in memory. Like everything stored in memory, it needs an address, or we wouldn't be able to find it. Lets say i
ends up at address 0x12345678 and its buddy j
with value 6 ends up just after it. Assuming a 32-bit CPU where int is 4 bytes and pointers are 4 bytes, then the variables are stored in physical memory like this:
Address Data Meaning
0x12345678 00 00 00 05 // The variable i
0x1234567C 00 00 00 06 // The variable j
Now we want to point at these variables. We create one pointer to int, int* ip1
, and one int* ip2
. Like everything in the computer, these pointer variables get allocated somewhere in memory too. Lets assume they end up at the next adjacent addresses in memory, immediately after j
. We set the pointers to contain the addresses of the variables previously allocated: ip1=&i;
("copy the address of i into ip1") and ip2=&j
. What happens between the lines is:
Address Data Meaning
0x12345680 12 34 56 78 // The variable ip1(equal to address of i)
0x12345684 12 34 56 7C // The variable ip2(equal to address of j)
So what we got were just yet some 4 byte chunks of memory containing numbers. There's no mystical or magical arrows anywhere in sight.
In fact, just by looking at a memory dump, we can't tell whether the address 0x12345680 contains an int
or int*
. The difference is how our program chooses to use the contents stored at this address. (The task of our program is actually just to tell the CPU what to do with these numbers.)
Then we add yet another level of indirection with int** ipp = &ip1;
. Again, we just get a chunk of memory:
Address Data Meaning
0x12345688 12 34 56 80 // The variable ipp
The pattern does seem familiar. Yet another chunk of 4 bytes containing a number.
Now, if we had a memory dump of the above fictional little RAM, we could manually check where these pointers point. We peek at what's stored at the address of the ipp
variable and find the contents 0x12345680. Which is of course the address where ip1
is stored. We can go to that address, check the contents there, and find the address of i
, and then finally we can go to that address and find the number 5.
So if we take the contents of ipp, *ipp
, we will get the address of the pointer variable ip1
. By writing *ipp=ip2
we copy ip2 into ip1, it is equivalent to ip1=ip2
. In either case we would get
Address Data Meaning
0x12345680 12 34 56 7C // The variable ip1
0x12345684 12 34 56 7C // The variable ip2
(These examples were given for a big endian CPU)
Upvotes: 13
Reputation: 106012
ipp
can hold a value of (i.e point to) a pointer to pointer type object. When you do
ipp = &ip2;
then the ipp
contains the address of the variable (pointer) ip2
, which is (&ip2
) of type pointer to pointer. Now the arrow of ipp
in second pic will point to ip2
.
Wiki says:
The *
operator is a dereference operator operates on pointer variable, and returns an l-value (variable) equivalent to the value at pointer address. This is called dereferencing the pointer.
Applying *
operator on ipp
derefrence it to a l-value of pointer to int
type. The dereferenced l-value *ipp
is of type pointer to int
, it can hold the address of an int
type data. After the statement
ipp = &ip1;
ipp
is holding the address of ip1
and *ipp
is holding the address of (pointing to) i
. You can say that *ipp
is an alias of ip1
. Both **ipp
and *ip1
are alias for i
.
By doing
*ipp = ip2;
*ipp
and ip2
both points to same location but ipp
is still pointing to ip1
.
What *ipp = ip2;
does actually is that it copies the contents of ip2
(the address of j
) to ip1
(as *ipp
is an alias for ip1
), in effect making both pointers ip1
and ip2
pointing to the same object (j
).
So, in the second figure, arrow of ip1
and ip2
is pointing to j
while ipp
is still pointing to ip1
as no modification is done to change the value of ipp
.
Upvotes: 0
Reputation: 5359
*ipp = ip2;
implies:
Assign ip2
to the variable pointed to by ipp
. So this is equivalent to:
ip1 = ip2;
If you want the address of ip2
to be stored in ipp
, simply do:
ipp = &ip2;
Now ipp
points to ip2
.
Upvotes: 1
Reputation: 1
Because you are changing the pointer of *ipp
. It means
ipp
(varaiable name)----go inside.ipp
is address of ip1
.*ipp
so go to (adress of inside) ip1
. Now we are at ip1
.
*ipp
(i.e.ip1
) = ip
2.
ip2
contain address of j
.so ip1
content will be replace by contain of ip2(i.e. address of j),
WE ARE NOT CHANGING ipp
CONTENT.
THAT'S IT.
Upvotes: 1
Reputation: 114
Because when you say
*ipp = ip2
you're saying the 'object pointed by ipp
' to point the direction of memory that ip2
is pointing.
You're not saying ipp
to point ip2
.
Upvotes: 5
Reputation: 40558
Forget for a second about the pointing analogy. What a pointer really contains is a memory address. The &
is the "address of" operator - i.e. it returns the address in memory of an object. The *
operator gives you the object a pointer refers to, i.e. given a pointer containing an address, it returns the object at that memory address. So when you do *ipp = ip2
, what you are doing is *ipp
get the object at the address held in ipp
which is ip1
and then assign to ip1
the value stored in ip2
, which is the address of j
.
Simply
&
--> Address of
*
--> Value at
Upvotes: 144
Reputation: 24812
My question is: Why in the second picture, ipp is still point to ip1 but not ip2?
you placed nice pictures, I'm going to try to make nice ascii art:
Like @Robert-S-Barnes said in his answer: forget about pointers, and what points to what, but think in terms of memory. Basically, an int*
means that it contains the address of a variable and an int**
contains the address of a variable that contains the address of a variable. Then you can use the pointer's algebra to access the values or the addresses: &foo
means address of foo
, and *foo
means value of the address contained in foo
.
So, as pointers is about dealing with memory, the best way to actually make that "tangible" is to show what the pointers algebra does to the memory.
So, here's your program's memory (simplified for the purpose of the example):
name: i j ip1 ip2 ipp
addr: 0 1 2 3 4
mem : [ | | | | ]
when you do your initial code:
int i = 5, j = 6;
int *ip1 = &i, *ip2 = &j;
here's how your memory looks like:
name: i j ip1 ip2
addr: 0 1 2 3
mem : [ 5| 6| 0| 1]
there you can see ip1
and ip2
gets the addresses of i
and j
and ipp
still does not exists.
Don't forget that addresses are simply integers stored with a special type.
Then you declare and defined ipp
such as:
int **ipp = &ip1;
so here's your memory:
name: i j ip1 ip2 ipp
addr: 0 1 2 3 4
mem : [ 5| 6| 0| 1| 2]
and then, you're changing the value pointed by the address stored in ipp
, which is
the address stored in ip1
:
*ipp = ip2;
the program's memory is
name: i j ip1 ip2 ipp
addr: 0 1 2 3 4
mem : [ 5| 6| 1| 1| 2]
N.B.: as int*
is a special type, I prefer to always avoid declaring multiple pointers on the same line, as I think the int *x;
or int *x, *y;
notation can be misleading. I prefer to write int* x; int* y;
HTH
Upvotes: 5
Reputation: 32512
If you add the dereference operator *
to the pointer, you redirect from the pointer to the pointed-to object.
Examples:
int i = 0;
int *p = &i; // <-- N.B. the pointer declaration also uses the `*`
// it's not the dereference operator in this context
*p; // <-- this expression uses the pointed-to object, that is `i`
p; // <-- this expression uses the pointer object itself, that is `p`
Therefore:
*ipp = ip2; // <-- you change the pointer `ipp` points to, not `ipp` itself
// therefore, `ipp` still points to `ip1` afterwards.
Upvotes: 4
Reputation: 71070
Because you changed the value pointed to by ipp
not the value of ipp
. So, ipp
still points to ip1
(the value of ipp
), ip1
's value is now the same as ip2
's value, so they both point to j
.
This:
*ipp = ip2;
is the same as:
ip1 = ip2;
Upvotes: 43
Reputation: 660088
Like most beginner questions in the C tag, this question can be answered by going back to first principles:
&
operator turns a variable into a pointer.*
operator turns a pointer into a variable.(Technically I should say "lvalue" instead of "variable", but I feel it is more clear to describe mutable storage locations as "variables".)
So we have variables:
int i = 5, j = 6;
int *ip1 = &i, *ip2 = &j;
Variable ip1
contains a pointer. The &
operator turns i
into a pointer and that pointer value is assigned to ip1
. So ip1
contains a pointer to i
.
Variable ip2
contains a pointer. The &
operator turns j
into a pointer and that pointer is assigned to ip2
. So ip2
contains a pointer to j
.
int **ipp = &ip1;
Variable ipp
contains a pointer. The &
operator turns variable ip1
into a pointer and that pointer value is assigned to ipp
. So ipp
contains a pointer to ip1
.
Let's sum up the story so far:
i
contains 5j
contains 6ip1
contains "pointer to i
"ip2
contains "pointer to j
"ipp
contains "pointer to ip1
"Now we say
*ipp = ip2;
The *
operator turns a pointer back into a variable. We fetch the value of ipp
, which is "pointer to ip1
and turn it into a variable. What variable? ip1
of course!
Therefore this is simply another way of saying
ip1 = ip2;
So we fetch the value of ip2
. What is it? "pointer to j
". We assign that pointer value to ip1
, so ip1
is now "pointer to j
"
We only changed one thing: the value of ip1
:
i
contains 5j
contains 6ip1
contains "pointer to j
"ip2
contains "pointer to j
"ipp
contains "pointer to ip1
"Why does
ipp
still point toip1
and notip2
?
A variable changes when you assign to it. Count the assignments; there cannot be more changes to variables than there are assignments! You start by assigning to i
, j
, ip1
, ip2
and ipp
. You then assign to *ipp
, which as we've seen means the same as "assign to ip1
". Since you didn't assign to ipp
a second time, it didn't change!
If you wanted to change ipp
then you'll have to actually assign to ipp
:
ipp = &ip2;
for instance.
Upvotes: 22
Reputation: 7424
Considere each variable represented like this:
type : (name, adress, value)
so your variables should be represented like this
int : ( i , &i , 5 ); ( j , &j , 6); ( k , &k , 5 )
int* : (ip1, &ip1, &i); (ip1, &ip1, &j)
int** : (ipp, &ipp, &ip1)
As the value of ipp
is &ip1
so the inctruction:
*ipp = ip2;
changes the value at the addess &ip1
to the value of ip2
, which means ip1
is changed:
(ip1, &ip1, &i) -> (ip1, &ip1, &j)
But ipp
still:
(ipp, &ipp, &ip1)
So the value of ipp
still &ip1
which means it still points to ip1
.
Upvotes: 3
Reputation: 2898
hope this piece of code can help.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int i = 5, j = 6, k = 7;
int *ip1 = &i, *ip2 = &j;
int** ipp = &ip1;
printf("address of value i: %p\n", &i);
printf("address of value j: %p\n", &j);
printf("value ip1: %p\n", ip1);
printf("value ip2: %p\n", ip2);
printf("value ipp: %p\n", ipp);
printf("address value of ipp: %p\n", *ipp);
printf("value of address value of ipp: %d\n", **ipp);
*ipp = ip2;
printf("value ipp: %p\n", ipp);
printf("address value of ipp: %p\n", *ipp);
printf("value of address value of ipp: %d\n", **ipp);
}
it outputs:
Upvotes: 21
Reputation: 12658
Very beginning you set,
ipp = &ip1;
Now dereference it as,
*ipp = *&ip1 // Here *& becomes 1
*ipp = ip1 // Hence proved
Upvotes: 3
Reputation: 2738
Notice the assignments:
ipp = &ip1;
results ipp
to point to ip1
.
so for ipp
to point to ip2
, we should change in the similar manner,
ipp = &ip2;
which we are clearly not doing. Instead we are changing the value at address pointed by ipp
.
By doing the folowing
*ipp = ip2;
we are just replacing the value stored in ip1
.
ipp = &ip1
, means *ipp = ip1 = &i
,
Now, *ipp = ip2 = &j
.
So, *ipp = ip2
is essentially same as ip1 = ip2
.
Upvotes: 8
Reputation: 544
If you'd want ipp
to point to ip2
, you'd have to say ipp = &ip2;
. However, this would leave ip1
still pointing to i
.
Upvotes: 3
Reputation: 22624
ipp = &ip1;
No later assignment has changed the value of ipp
. This is why it still points to ip1
.
What you do with *ipp
, i.e., with ip1
, does not change the fact that ipp
points to ip1
.
Upvotes: 5