Why is 90
the output value of y
and q
? I just do p=q
. How come the value of q
is changed?
int main() { int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p = q; *p = 90; cout << x << " " << y << endl; cout << *p << " " << *q << endl; cout << "Address of p = " << p << endl; cout << "Address of q = " << q << endl; return 0; }
The output is:
35 90 90 90 Address of p = 0xbffa83c0 Address of q = 0xbffa83c0
Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x . Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer.
When pointer assignment occurs, any previous association between the pointer object and a target is terminated. Pointers can also be assigned for a pointer structure component by execution of a derived-type intrinsic assignment statement or a defined assignment statement.
A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.
A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.
I'd like to share a general technique that I used to learn how pointers work when I was starting out. If you apply it to your problem, you'll see the answer as plain as day.
Get a big sheet of graph paper and lay it lengthwise on the table in front of you. This is your computer's memory. Each box represents one byte. Pick a row, and place the number '100' below the box at far left. This is "the lowest address" of memory. (I chose 100 as an arbitrary number that isn't 0, you can choose another.) Number the boxes in ascending order from left to right.
+---+---+---+---+---+-- | | | | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now, just for the moment, pretend an int is one byte in size. You are an eight-bit computer. Write your int a
into one of the boxes. The number below the box is its address. Now choose another box to contain int *b = &a
. int *b
is also a variable stored somewhere in memory, and it is a pointer that contains &a
, which is pronounced "a's address".
int a = 5; int *b = &a;
a b +---+---+---+---+---+-- | 5 | |100| | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now you can use this model to visually work through any other combinations of values and pointers that you see. It is a simplification (because as language pedants will say, a pointer isn't necessarily an address, and memory isn't necessarily sequential, and there's stack and heap and registers and so on), but it's a pretty good analogy for 99% of computers and microcontrollers.
So in your case,
int x = 35; int y = 46;
x y +---+---+---+---+---+-- | 35| 46| | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
int *p = &x; int *q = &y;
x y p q +---+---+---+---+---+-- | 35| 46|100|101| | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
p = q;
x y p q +---+---+---+---+---+-- | 35| 46|101|101| | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
*p = 90;
x y p q +---+---+---+---+---+-- | 35| 90|101|101| | ... +---+---+---+---+---+-- 100 101 102 103 104 ...
Now what is *p
? What is *q
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With