//数组的初始化方法 # include<stdio.h> # include<stdlib.h> int main() { int a[3][4]; int num = 0; //方法一: /* for (int i = 0; i < 3 * 4; i++) { a[i / 4][i % 4] = num; num++; } */ //方法二: /* printf("start...\n"); for (int *p = &a[0][0]; p < &a[0][0] + 12; p++)//原理是数组在内存中是线性排列的 { *p = num;//指针的线性方式初始化数组 num++; } */ //方法三: /* int a[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; */ //方法四: for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { a[i][j] = num; num++; } } //打印 for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { printf("%4d", a[i][j]); } printf("\n"); } getchar(); }
时间: 2024-12-16 05:23:37