// console3_5.cpp // consts, ptr, const ptrs, ptrs to const... // How much wood would a wood chuck chuck if a wood chuck could chuck wood? #include int z = 0xFFFF; int main( int argc, char *argv[] ) { cout << "Consts and Pointers" << endl; int a = 0xAAAA; // 'a' is an integer int *b = &a; // 'b' is a ptr to an integer const int c = 0xC; // 'c' is an integer constant int * const d = &a; // 'd' is a const ptr to an int const int * e = &a; // 'e' is a ptr to an integer constant const int * const f = &a; // 'f' is a const ptr to an integer constant cout << hex; // const int c = 0xC; //c = 0xD; // ERROR! // int * const d = &a; cout << "Before, *d = 0x" << *d << endl; *d = *d + 1; // OK because where it points is constant cout << "After, *d = 0x" << *d << endl; //d = &z; // ERROR, cuz where we point is constant, not what we point at // const int * e = &a; //*e = *e + 1; // ERROR, What it points to is constant, // not where it points cout << "Before, *e = 0x" << *e << endl; e = &z; // Where it points is NOT constant! cout << "After, *e = 0x" << *e << endl; // const int * const f = &a; // *f = *f + 1; // ERROR, can't change what it points to! // f = &z; // ERROR, can't change where it points either! // f is an alias to a Read Only Memory location! cout << "ROM, *f = 0x" << *f << endl; return 0; }