Quote:
Originally Posted by jmakin
Maybe someone can help solidify my understanding of pointers in C++.
In java, for instance, all variables are "pointers" (not really i know). When I say x = y, you're merely making x refer to y.
In C++, that same statement actually creates a copy of variable y into variable x.
So, my question is, in C++, how the **** do variables work? If I have a variable declared and initialized like -
int x = 10;
int * p = &x;
*p = 4; // changes x to 4
(the value that p points to is changed to 4 is how i read that in my head)
would *x be the same essentially as x or am I misunderstanding completely?
To add to what was already said: when you declare "int x = 10", what happens is that the program makes room in the
stack for an int, and the variable x is located at that memory address on the stack. *x, if the compiler would let you do it (to go beyond what muttiah said) is actually going to lead to some sort of memory access violation, because *x means to take the value of x and interpret it as a memory location (a pointer), and read from that location. But the location 10 is not very likely to be valid given how memory is laid out in most (all?) operating systems.
On the other hand, &x means to get the memory address where the value in the variable x is stored, which in this case is the location on the stack that was allocated when you declared the variable.
In the same way, when you declare "int *p", you are also created space on the stack, this time, rather than space for an integer, you are reserving enough space for a memory address, and declaring that the value stored at that address will be an int. Hence "pointer to int p".
int *p = &x
allocates enough space for a pointer, and in that space places, as a value, the memory address of the variable x
*p = 4;
"*p" means to take the
value of p, which is the memory address &x, and use it as a memory address in the assignment. This is what it means for p to point at x. As long as p == &x, *p references the same memory location as &x
The thing that gets tricky is to remember that the value of the pointer, which is the address of the variable x (&x), is distinct from the location in memory of p itself, i.e &p. &p != &x, but *p == &x.
The name of a variable always references the "value" of the variable, but if the variable is of type "int *" then the value is itself a memory address, whereas with a type "int" the value is an integer. The memory address of a pointer is distinct from its value, which may point somewhere else.
x: the value of the variable
&x: the memory address where the variable lives
*x: the memory address designated by the data in the value of the variable.
*x is a special case only in that it won't lead to useful results if the type of x is not a pointer, but under the hood a memory address is just a number. It may be a 64bit number or 32bit number, but you can cast between the appropriate size unsigned integer and a memory address. Knowing that may help to understand what it means for the "value" of a pointer to be a memory address. It is just a number interpreted with a special meaning