//在堆中開闢一個4×5的二維int陣列#include <stdio.h>#include <malloc.h>int **p;int i,j;void main() { p=(int **)malloc(4*sizeof(int *)); if (NULL==p) return; for (i=0;i<4;i++) { p[i]=(int *)malloc(5*sizeof(int)); if (NULL==p[i]) return; } for (i=0;i<4;i++) { for (j=0;j<5;j++) { p[i][j]=i*5+j; } } for (i=0;i<4;i++) { for (j=0;j<5;j++) { printf(" %2d",p[i][j]); } printf("\n"); } for (i=0;i<4;i++) { free(p[i]); } free(p);}// 0 1 2 3 4// 5 6 7 8 9// 10 11 12 13 14// 15 16 17 18 19
//在堆中開闢一個4×5的二維int陣列#include <stdio.h>#include <malloc.h>int **p;int i,j;void main() { p=(int **)malloc(4*sizeof(int *)); if (NULL==p) return; for (i=0;i<4;i++) { p[i]=(int *)malloc(5*sizeof(int)); if (NULL==p[i]) return; } for (i=0;i<4;i++) { for (j=0;j<5;j++) { p[i][j]=i*5+j; } } for (i=0;i<4;i++) { for (j=0;j<5;j++) { printf(" %2d",p[i][j]); } printf("\n"); } for (i=0;i<4;i++) { free(p[i]); } free(p);}// 0 1 2 3 4// 5 6 7 8 9// 10 11 12 13 14// 15 16 17 18 19