Hello everyone! I've just started to learn C++ and have a question... I've written the following code:
which produces in my computer the following output:Code:#include <iostream.h> int main() { const int i=1; int* pi; //pi = &i; Error, since pi isn't declared as a pointer\ which points to a constant pi = (int*) &i; // Address of constant i allocated \ to pointer pi with a cast *pi = 2; cout << "Address of i:\t\t" << &i << endl; cout << "pi points to:\t\t" << pi << endl; cout << "Value of i:\t\t" << i << endl; cout << "Value pointed to by pi:\t" << *pi << endl; return 0; }
Address of i: 0x0012FF7C
pi points to: 0x0012FF7C
Value of i: 1
Value pointed to by pi: 2
Now, my question is, why, if the address of i and the address pointed to by pi are the same, can I (apparently) have two different values stored in this same address? I'm sure I'm missing something here, maybe I have some conceptual misconception on the subject pointers... Any help would be much appreciated.



LinkBack URL
About LinkBacks


