我有2个数组如下 -
int **data;
int ***count;
进行一些分析后,我想做以下任务-
count[i][j][k] = data[i][j];
但是,我不断收到分段错误,我认为这与一些指针分配问题有关 - 谁能建议我如何进行此分配?
- data[i][j] = 0/1/2 的典型值。
ZALLOC的定义:
#define ZALLOC(item,n,type) if ((item = (type *)calloc((n),sizeof(type))) == NULL) fatalx("Unable to allocate %d unit(s) for item\n",n)
// memory assignment
int **data;
int nrow, ncol;
ZALLOC(data, ncol, int *);
for (index = 0; index < ncol; index++)
{
ZALLOC(data[index], nrow, int);
}
int g=0, index1=0, index2=2;
data[index1][index2] = g;
int ***count;
int dim1 = 100, dim2 = 1, dim3=2;
ZALLOC(count, dim1, int **);
for (i = 0; i < dim1; i++)
{
ZALLOC(count[i], dim2, int *);
for (j = 0; j < dim2; j++)
{
ZALLOC(count[i][j], dim3, int);
}
}
// Initialize
for (i = 0; i < dim1; i++)
{
for (j = 0; j < dim2; j++)
{
for (k = 0; k < dim3; k++)
{
count[i][j][k] = 0;
}
}
}
// Assignment
count[0][1][2] = data[1][2];