2
C에서이 방법으로 2D 배열을 동적으로 할당 할 수 있습니까?고정 크기 배열에 대한 포인터로 2D 배열 할당
//fixed size
int (*arr2d)[9][9]=malloc(sizeof(int[9][9]));
//variable size
int x=5,y=3;
int (*arr2d)[x][y]=malloc(sizeof(int[x][y]));
//assignment
(*arr2d)[0][1]=2;
편집 :함으로써 "유효"나는 반대로 어떤 함정이있는, 의미에 :
당신이malloc
호출 (당신을 만족 할 수있는 것보다 더 많은 메모리를 요청하는 경우
int i,x=5,y=10;
int **arr2d=malloc(sizeof(int*[x]));
for(i=0;i < x;i++)
arr2d[i]=malloc(sizeof(int[y]));
arr2d[0][0]=5;