// testing 2D array access using a pointer // #include int main( void) { int a[3][4]; // = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} }; // 2D array, on stack for( int row = 0; row < 3; ++row) { for( int col = 0; col < 4; ++col) printf( " %3i", a[row][col]); putchar('\n'); } int *p = &a[0][0]; // pointer to first element for( int i = 0; i < 12; ++i) printf( " %3i", p[i]); putchar('\n'); // p[row*4+col] is the same as a[row][col] for 2D array with 4 columns // for( int row = 0; row < 3; ++row) { for( int col = 0; col < 4; ++col) printf( " %3i", p[row*4+col]); putchar('\n'); } } /* sample run: 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 */