21.10. Exercises: Memory Access

Predict the output of Example 21.7. Then build and run it. Explain the output. If it differs from your prediction, explain the difference(s).

[ fromfile: memoryaccess.xml id: None ]

Example 21.7. src/arrays/arrayVSptr.cpp

#include <iostream>
using namespace std;

int main()  {
   int a[] = {12, 34, 56, 78};
   cout << a << "\t" << &a[1] - a << endl;
   int x = 99;
//   a = &x;
   int* pa;
   cout << pa << endl;
   pa = &x;
   cout << pa << "\t" << pa - &a[3] << endl;
   cout << a[4] << "\t" << a[5] << endl;
   cout << *(a + 2) << "\t" << sizeof(int) <<endl;
   void* pv = a;
   cout << pv << endl;
   int* pi = static_cast<int*>(pv);
   cout << *(pi + 2) << endl;
   return 0;
}