Hi, I'm inspired with the similar thread on this board.
I know how to allocate 2D array:
Code:
int main (void)
{
int rows, cols, i, j;
int** matrix;
int* rawdata;
scanf("%d %d",&rows, &cols);
rawdata = malloc(rows * cols * sizeof(int));
matrix = malloc(rows * sizeof(int*));
for (i = 0; i < rows; ++i)
{
matrix[i] = &rawdata[i * cols];
}
for (i = 0; i < rows; ++i)
{
for (j = 0; j < cols; ++j)
{
scanf("%d",&matrix[i][j]);
}
}
free (matrix);
free (rawdata);
return 0;
}
Now, I don't know is it possible to extend this logic and to allocate 3D array. I tried something, but I cannot wrap my mind.
here's my shot:
Code:
int main (void)
{
int a, b, c, i, j,k;
int ***array_3D;
int* rawdata;
int** matrix;
scanf ("%d %d %d", &a, &b, &c);
rawdata = malloc (a * b * c * sizeof(int));
matrix = malloc (a * sizeof(int*));
array_3D = malloc(c * sizeof(int**));
for (i = 0; i < a; ++i)
{
matrix[i] = & rawdata[i * b];
}
/* Now this is where all is messed up
for (i = 0; i < c; ++i)
{
/*Possible nested loop, but ???*/
}
for (i = 0; i < a; ++i)
for (j = 0; j < b; ++j)
for (k = 0; k < c; ++k)
scanf("%d",&array_3D[i][j][k]);
/*appropriate cleanup*/
return 0;
}
Can you help me write it?
I tried to do it myself but I didn't have success and I'm asking for your help.
Thanks
- Micko