简体   繁体   中英

Why do I get different pointer values after an assignment?

so I am currently learning C++ and trying to get a hang of the relationship between arrays and pointers. The code below shows the assignments I have made.

int A[4] = {20, 30, 40, 50};
int p = A[0];
int* q = &p;

Now, if I print out the pointer values, I get this:

A == 0x7ffda1cb1ea0
&A == 0x7ffda1cb1ea0
&A[0] == 0x7ffda1cb1ea0
&p == 0x7ffda1cb1e94
q == 0x7ffda1cb1e94
&q == 0x7ffda1cb1e98

I don't really understand why the values for &A[0] and &p are different, even though the assignment ist p = A[0]. I also don't understand how there are only 4 bytes between q and &q, even though the size of q is shown as 8. Any help would be greatly appreciated.

I don't really understand why the values for &A[0] and &p are different

They are different objects in memory, so they have different addresses. A[4] is one object, p is another one and q is another one. The assignment makes them hold the same value, but they're still two different objects. You can remove the assignment and the result will be the same.

I also don't understand how there are only 4 bytes between q and &q, even though the size of q is shown as 8

q is set to the address of p , and &p gives you the address of q . The result of 4 makes sense, because that's how big p is. That's why q comes 4 after p , because p takes up 4 bytes.


Perhaps it's more clear if you try the same thing with a reference:

int A[4] = { 20, 30, 40, 50 };
int &p = A[0]; // note the "&", this makes p a reference
std::cout << "&A[0]: " << &A[0] << std::endl << "&p:    " << &p << std::endl;

For me, this results in:

&A[0]: 008FFD90
&p:    008FFD90

Now p isn't a separate object that can hold a copy of a data, but an actual reference to the same place.

I don't really understand why the values for &A[0] and &p are different

A[0] and p are separate objects. Each object has an unique address during their life time (except certain cases involving subobjects). The life time of A[0] and p overlap. Therefore &A[0] and &p cannot possibly be the same.

even though the assignment ist p = A[0]

Assignment has no effect on where an object is stored. It can only affect the value that is stored in the object.

also don't understand how there are only 4 bytes between q and &q, even though the size of q is shown as 8

The size of q is irrelevant, since it points to a lower address. The size of p happens to be 4:

 /-offset from p
/  object in memory
00 p (first byte) \
01 p               |
02 p               |
03 p               |
04 q (first byte)  | Distance between p and q: 4
05 q 
06 q
07 q
08 q
09 q
10 q
11 q

When you are doing int p = A[0] you are simply assigning the value of A[0] to p. They are both different objects and are stored at different locations. The value of &p will not change if you change the value of p.

The gap of 4 is due to the size of p it is not related to the size of q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM