// arabc1.cpp // array of pointers to arrays // superficially similar to 2-D array, but not the same #include #include using namespace std; int main() { // some arrays of ints // dummy vbls to keep arrays from being contiguous int a[] = {1, 2, 3}; int j1; int b[] = {10, 20, 30}; int j2; int j3; int c[] = {100, 200, 300}; int j4; int j5; int j6; // an array of pointers to the arrays above int *ar[] = {a, b, c}; cout << " a = " << a << endl; cout << " b = " << b << endl; cout << " c = " << c << endl; cout << endl; cout << " b[1] = " << b[1] << endl; cout << " b+1 = " << b+1 << endl; cout << " (b+1)[1] = " << (b+1)[1] << endl; cout << endl; cout << " 1[b] = " << 1[b] << endl; cout << endl; cout << " ar = " << ar << endl; cout << " ar[1] = " << ar[1] << endl; cout << endl; cout << " *ar[1] = " << *ar[1] << endl; cout << " *(ar[1]) = " << *(ar[1]) << endl; cout << " (*ar)[1] = " << (*ar)[1] << endl; cout << endl; // accessing the inner arrays indirectly through the outer array cout << "ar[1][1] = " << ar[1][1] << endl; cout << endl; // Order of operations: // . member // -> member indirect through pointer // [] subscript // * dereference cout << "ar = " << ar << endl; cout << "ar+1 = " << ar+1 << endl; cout << "(ar+1)[1] = " << (ar+1)[1] << endl; cout << "*(ar+1) " << *(ar+1) << endl; cout << endl; cout << "*((ar+1)[1]) = " << *((ar+1)[1]) << endl; cout << "(*(ar+1))[1] = " << (*(ar+1))[1] << endl; cout << "*(ar+1)[1] = " << *(ar+1)[1] << endl; return 0; } /* Sample output: a = 0xffbefc10 b = 0xffbefbf8 c = 0xffbefbe0 b[1] = 20 b+1 = 0xffbefbfc (b+1)[1] = 30 1[b] = 20 ar = 0xffbefbc0 ar[1] = 0xffbefbf8 *ar[1] = 10 *(ar[1]) = 10 (*ar)[1] = 2 ar[1][1] = 20 ar = 0xffbefbc0 ar+1 = 0xffbefbc4 (ar+1)[1] = 0xffbefbe0 *(ar+1) 0xffbefbf8 *((ar+1)[1]) = 100 (*(ar+1))[1] = 20 *(ar+1)[1] = 100 */