Pointer array/array pointer, pointer Array
# Include <iostream> using namespace std; // note how pointer arrays and array pointers point to two-dimensional arrays # include <stdio. h> main () {static int m [3] [4] = }; /* define a two-dimensional array m and initialize */int (* p) [4]; // array pointer p is a pointer pointing to a one-dimensional array, each one-dimensional array has four int elements: int I, j; int * q [3]; // the pointer array q is an array, and the array element is a pointer, the three int pointers p = m; // p are pointers, which can direct to the two-dimensional array printf ("-- array pointer output element -- \ n"); for (I = 0; I <3; I ++)/* outputs the values of each element in a two-dimensional array */{for (j = 0; j <4; j ++) {printf ("% 3d", * (p [I] + j); // printf ("% 3d", * (p + I) + j);} printf ("\ n");} printf ("\ n"); for (I = 0; I <3; I ++, p ++) // p can be viewed as a row pointer {printf ("% 3d", ** p); // the first element of each row printf ("% 3d ", * (* p + 1); // the second element of each row, printf ("% 3d", * (* p + 2 )); // printf ("% 3d", * (* p + 3), the third element of each row )); // printf ("\ n");} printf ("\ n"); printf ("\ n"); printf ("-- pointer array output element -- \ n "); for (I = 0; I <3; I ++) q [I] = m [I]; // q is an array, element q [I] is a pointer for (I = 0; I <3; I ++) {for (j = 0; j <4; j ++) {printf ("% 3d", q [I] [j]); // q [I] [j] can be changed to * (q [I] + j )} printf ("\ n");} printf ("\ n"); printf (""); q [0] = m [0]; for (I = 0; I <3; I ++) {for (j = 0; j <4; j ++) {printf ("% 3d ", * (q [0] + j + 4 * I);} printf ("\ n");} printf ("\ n ");}
Output result:
This code is well written .....