方法一:
#include<stdio.h> #include<stdlib.h> int main() { int(*p)[4] = (int (*)[4])malloc(12 * sizeof(int));//数组指针 for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { *(*(p + i) +j) = i * 4 + j + 1 ; } } for ( i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { printf("%d\t", *(*(p + i) + j)); } } printf("\n"); system("pause"); return 0; }
方法二:
#include<stdio.h> #include<stdlib.h> int main() { int i = 0; int j = 0; int **p = (int **)malloc(3 * sizeof(int)); for (i = 0; i < 3; i++) { *(p + i) = (int *)malloc(4 * sizeof(int)); } for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { p[i][j] = 4 * i + j + 1; } } for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { printf("%d ", *(*(p + i) + j)); } } system("pause"); return 0; }
时间: 2024-11-10 01:36:32