//以int为例,以10*20的二维数组为例
#define nrow 10
#define ncol 20
void test1(int (*a)[ncol]){
cout << a[6][6]<<endl;
}
void test2(int a[][ncol]){
cout << a[6][6]<<endl;
}
void test3(int **a){
cout << a[6][6]<<endl;
}
int main(){
int (*a)[ncol] = (int (*)[ncol])malloc(nrow * sizeof(*a));//sizeof(int[ncol])
int i,j,k=0;
for(i=0; i<nrow; ++i)
for(j=0; j<ncol; ++j)
a[i][j] = k++;
test1(a);//126
test2(a);//126
test3(a);//error
free(a);
//对比静态数组
int t[nrow][ncol] = {0};
test1(t);//0
test2(t);//0
test3(t);//error
return 0;
}
//以int为例,以10*20的二维数组为例
#define nrow 10
#define ncol 20
void test1(int (*a)[ncol]){
cout << a[6][6]<<endl;
}
void test2(int a[][ncol]){
cout << a[6][6]<<endl;
}
void test3(int **a){
cout << a[6][6]<<endl;
}
int main(){
int **a = (int **)malloc(nrow * sizeof(*a));//sizeof(int*)
int i,j,k=0;
for(i=0; i<nrow; ++i)
*(a+i) = (int *)malloc(ncol * sizeof(**a));//sizeof(int**)
//a[i] = (int *)malloc(ncol * sizeof(**a));
for(i=0; i<nrow; ++i)
for(j=0; j<ncol; ++j)
*(*(a+i)+j) = k++;
//a[i][j] = k++;
test1(a);//error
test2(a);//error
test3(a);//126
for(i=0; i<nrow; ++i)
free(a[i]);
free(a);
//对比静态数组
int t[nrow][ncol] = {0};
test1(t);//0
test2(t);//0
test3(t);//error
return 0;
}
www.xingrong0804.com
浙公网安备 33010602011771号