malloc二維陣列的2種方法
#include <malloc.h> #include <stdio.h> int main() { int **a = malloc(sizeof(int)*3); a[0]= malloc(sizeof(int)*2); a[1]= malloc(sizeof(int)*2); a[2]= malloc(sizeof(int)*2); a[0][0] =1; a[0][1] =2; a[1][0] =3; a[1][1] = 4; a[2][0] =5; a[2][1] = 6; printf("%d\t%d\t%d\t%d\t%d\t%d\n",a[0][0],a[0][1],a[1][0],a[1][1],a[2][0],a[2][1]); free(a[0]); free(a[1]); free(a[2]); free(a); }
#include <malloc.h>
#include <stdio.h>
int main()
{
int (*a)[2] = malloc(sizeof(int)*3*2);
a[0][0] =1;
a[0][1] =2;
a[1][0] =3;
a[1][1] = 4;
a[2][0] =5;
a[2][1] = 6;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",a[0][0],a[0][1],a[1][0],a[1][1],a[2][0],a[2][1]);
free(a);
}