利用 malloc 建立動態大小的2D array
#include "stdafx.h" #include "stdlib.h" int _tmain(int argc, _TCHAR* argv[]) { int **Array; //array[m][n] int m=5; //row int n=3; //column int i=0; Array = (int **)malloc(m*sizeof(int *)); for (i = 0; i < m; i++) Array[i] = (int *)malloc(n*sizeof(int *)); printf("write 2D array-----\n"); for(int y = 0; y<5; y++) { for(int x = 0; x<3; x++) { Array[x][y] = x + y*n; printf("array[%d][%d] = %d ", y, x, x + y*n); } printf("\n"); } printf("\nread 2D array-----\n"); for(int y = 0; y<5; y++) { for(int x = 0; x<3; x++) { printf("array[%d][%d] = %d ",y,x,Array[x][y]); } printf("\n"); } return 0; }