I have the following code:
1 #include <iostream>
2 using namespace std;
3 int main()
5 int x = 7; // integer variable x contains value 7
6 int* ip = &x; // integer pointer variable ip points to address of variable x
7 int &y = x; // integer value at address at variable y is assigned value stored in variable x
9 cout << "The value of x is: " << x << endl; // value held in location referenced by variable x
10 cout << "The value of &x is: " << &x << endl; // address of variable x
11 cout << "The value where *ip points is: " << *ip << endl; // value held in location pointed at by pointer ip
12 cout << "The value of &ip is: " << &ip << endl; // dereferenced pointer?
13 cout << "The value of ip is: " << ip << endl; // address of pointer ip
14 cout << "The value of y is: " << y << endl; // value held in location referenced by variable y
15 cout << "The address where y is: " << &y << endl; // address of variable y
17 return 0;
The comments are for my benefit to make sure I understand pointers, dereferencing, and the relationship between these and variables.
I understand pointers to basically be variables that only "contain"/point to memory addresses. However, line 12, the "dereferenced pointer" is throwing me off. It displays a memory address different from the others from lines 10, 13, and 15. The other lines all display 7 to the console, as I expected. Does anyone have an explanation, and if so, is there an easy way to understand why this is occurring?