[c]代码库
/* 使用一维数组型指针作为二维数组形参 */
#include <stdio.h>
#define ROW_SIZE 3
#define COL_SIZE 3
void print_array(int (*p)[COL_SIZE], const int row) {
int i, j;
for (i = 0; i < row; ++i) {
for (j = 0; j < COL_SIZE; ++j)
printf("%4d", *(*p + j));
printf("\n");
++p;
}
}
void reverse(int (*p)[COL_SIZE], const int row) {
int i, j;
for (j = 0; j < COL_SIZE; ++j) {
for (i = 0; i < row; ++i)
printf("%4d", *(*(p + i) + j));
printf("\n");
}
}
int main(void) {
int array[ROW_SIZE][COL_SIZE] = { { 1, 2, 3 }, { 11, 12, 13 },
{ 21, 22, 23 } };
printf("Output the matrix:\n");
print_array(array, ROW_SIZE);
printf("Reverse the matrix:\n");
reverse(&array[0], ROW_SIZE);
return 0;
}