C语言数组:二位数组使用演示
#include <stdio.h> void show_2d_array(int array[][10], int rows) { int i, j; for (i = 0; i < rows; i++) for (j = 0; j < 10; j++) printf("array[%d][%d] = %d\n", i, j, array[i][j]); } int main(void) { int a[1][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; int b[2][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}}; int c[3][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}}; show_2d_array(a, 1); show_2d_array(b, 2); show_2d_array(c, 3); return 1; }
输出结果
array[0][0] = 1 array[0][1] = 2 array[0][2] = 3 array[0][3] = 4 array[0][4] = 5 array[0][5] = 6 array[0][6] = 7 array[0][7] = 8 array[0][8] = 9 array[0][9] = 10 array[0][0] = 1 array[0][1] = 2 array[0][2] = 3 array[0][3] = 4 array[0][4] = 5 array[0][5] = 6 array[0][6] = 7 array[0][7] = 8 array[0][8] = 9 array[0][9] = 10 array[1][0] = 11 array[1][1] = 12 array[1][2] = 13 array[1][3] = 14 array[1][4] = 15 array[1][5] = 16 array[1][6] = 17 array[1][7] = 18 array[1][8] = 19 array[1][9] = 20 array[0][0] = 1 array[0][1] = 2 array[0][2] = 3 array[0][3] = 4 array[0][4] = 5 array[0][5] = 6 array[0][6] = 7 array[0][7] = 8 array[0][8] = 9 array[0][9] = 10 array[1][0] = 11 array[1][1] = 12 array[1][2] = 13 array[1][3] = 14 array[1][4] = 15 array[1][5] = 16 array[1][6] = 17 array[1][7] = 18 array[1][8] = 19 array[1][9] = 20 array[2][0] = 21 array[2][1] = 22 array[2][2] = 23 array[2][3] = 24 array[2][4] = 25 array[2][5] = 26 array[2][6] = 27 array[2][7] = 28 array[2][8] = 29 array[2][9] = 30