/* Faan Tone Liu * Jan 22, 2003 * Demo program for comp 1672 * Introduces pointers, declaring and initializing pointers, using the * address of operator (&) and the dereferencing operator (*) */ #include using namespace std; int main() { int i(41); int *ip(0); // Good practice to ALWAYS initialize pointers to 0 // when you declare them // going back and forth between address and data using an int variable // This shows the use of the address-of operator (&) cout << "The value of i is " << i << endl; cout << "The address of i is " << &i << endl; // Using the dereferencing operator (*) to access the data stored at // a specific memory address. This example is not too useful! You // could just use the variable i itself, since * "undoes" & cout << "The value of *&i is " << *&i << endl; // going back and forth between address and data ip = &i; cout << "The value of ip is " << ip << endl; cout << "The data in the address ip points to is " << *ip << endl; // using a pointer to i to change data in i. ip points to i, so *ip // refers to the same int as i does *ip = 51; cout << "The value of i is now " << i << endl; // cout << *i; does not compile! cout << "The address of ip is " << &ip << endl; return 0; }