i****d 发帖数: 255 | 1 // 定义二维数组的指针
double **zzz;
int i, j, irow = 100, icol = 4;
// 有两种方式malloc
// 第一种
zzz = (double**) malloc( sizeof(double) * irow * icol) );
for(i=0; i
zzz[i] = (double *) malloc( sizeof(double) * icol) );
// 第二种
zzz = (double**) malloc( sizeof(double*) * irow) );
for(i=0; i
zzz[i] = (double *) malloc( sizeof(double) * icol) );
*******************************************
感觉第二种方式好理解:先定义一个指针数组zzz[irow];然后让里面的各个指针指向
一个个一维数组,即zzz[0]存储第0列的首地址,等等。
但第一种方式不好理解:感觉开辟了两份内存。
请指教! |
r*********r 发帖数: 3195 | |
d****i 发帖数: 4809 | 3 Exactly! The first method is wrong. You define a pointer array of size irow
* icol, which is not the case because you should only create a pointer array
of size irow, each of which is a pointer that points to the first element
of each column. |
l*******G 发帖数: 1191 | 4 can we use the second method to define multiple-dimension arrays (>2)?
i.e. something like
int idim1=100;
int idim2=200;
int idim3=300;
int idim4=10;
zzz = (double**) malloc( sizeof(double*) * idim1) );
for(i=0; i
zzz[i] = (double **) malloc( sizeof(double) * idim2) );
for(j=0; j
zzz[i][j]=(double **) malloc( sizeof(double) * idim3) );
for(k=0; k
zzz[i][j][k]=(double **) malloc( sizeof(double) * idim4) );
}
}
} |
a****l 发帖数: 8211 | 5 there is nothing like "can" or "can not". The only question is: is this
method suitable for your application?
【在 l*******G 的大作中提到】 : can we use the second method to define multiple-dimension arrays (>2)? : i.e. something like : int idim1=100; : int idim2=200; : int idim3=300; : int idim4=10; : zzz = (double**) malloc( sizeof(double*) * idim1) ); : for(i=0; i: zzz[i] = (double **) malloc( sizeof(double) * idim2) ); : for(j=0; j
|