//This is our third simple C++ program #include using namespace std; int main(){ // Pointer Example int i = 10; //an initialized int, will be automatically allocated cout << endl << "i is an automatic var allocated at addr:" << &i << endl << endl; int *ptr_to_i = &i; //initialize ptr_to_i with the address of i int *pi; //unititialized pointer to int int **ppi; //uninitialized pointer to pointer to int cout << "ptr_to_i is an automatic pointer to int variable." << " ptr_to_i has been allocated at addr:" << &ptr_to_i << endl << " and the value of ptr_to_i is:" << ptr_to_i << " which is the same as addr of i:" << &i << endl << endl; ppi = &ptr_to_i; //put the address of ptr_to_i into ppi //LINE 7 !!! pi = *ppi; // same as pi = ptr_to_i; // * is the "follow-the-pointer-arrow and evaluate that //location" operator. ppi points to ptr_to_i. following the //arrow once gives us ptr_to_i cout << "*ppi = " << **ppi << endl; //Following the ppi pointer once gives us ptr_to_i //as above. Following ptr_to_i once after that gives us i; return 0; } //The picture might make things clearer: // ___________ ___________ ___________ // ppi |___________|------> ptr_to_i|___________|------> |_____ 10 __| // (p. to p. to int) (pointer to int) (int) // ^ // | // | // ___________ | // pi |___________|-------------------------| // (pointer to int) Then type the following command at the command prompt: g++ -o pointers pointers.cc Then type at the command prompt pointers to execute your program. Try to reason about the output you get in each case and why you get it. If you don't understand an output, please ask me about it. Pointers are hard to understand so don't be shy about this. Comment line 7 above - i.e., use an unitialize pointer. You might get a SEGFAULT or BUS ERROR on the next line when trying to follow and unitialized pointer **ppi (i.e., your program will crash). -Cristiana