嘿,我正在做一个程序,该程序读取与3D数组的尺寸相对应的三个整数值(按行,列,深度的顺序),并应为此3D数组动态分配内存。然后最终打印与“ XOY轴”平行的3D阵列的2D截面。我在将//部分打印到XOY轴时得到Segmentation fault (core dumped)
#include <stdio.h>
#include <stdlib.h>
void read_matrices(int dim1, int dim2, int dim3, int ***A);
void XOY(int dim1, int dim2, int dim3, int ***A);
int main(int argc, char const *argv[])
{
/* code */
int dim1, dim2, dim3;
scanf("%d%d%d", &dim1, &dim2, &dim3);
int i,j;
int *** array = (int ***)malloc(dim1*sizeof(int**));
for (i = 0; i< dim1; i++) {
array[i] = (int **) malloc(dim2*sizeof(int *));
for (j = 0; j < dim2; j++) {
array[i][j] = (int *)malloc(dim3*sizeof(int));
}
}
read_matrices(dim1,dim2,dim3, array);
XOY(dim1,dim2,dim3, array);
return 0;
}
void read_matrices(int dim1, int dim2, int dim3, int ***A)
{
//getting input of the arrays A & B
int a,b,c;
for (a = 0; a < dim1; ++a)
{
for (b = 0; b < dim2; ++b)
{
for (c = 0; c < dim3; ++c)
{
scanf("%d", &A[a][b][c]);
}
}
}
}
void XOY(int dim1, int dim2, int dim3, int ***A)
{
printf("Section 1:\n");
for (int i = 0; i < dim1; ++i)
{
for (int i = 0; i < dim2; ++i)
{
printf("%d ", A[i][i][0]);
}
printf("\n");
}
printf("Section 2:\n");
for (int i = 0; i < dim2; ++i)
{
for (int i = 0; i < dim3; ++i)
{
/* code */
printf("%d ", A[0][i][i] );
}
printf("\n");
}
printf("Section 3:\n");
for (int i = 0; i < dim1; ++i)
{
for (int i = 0; i < dim3; ++i)
{
/* code */
printf("%d ", A[i][0][i] );
}
printf("\n");
}
}
编译警告:
for (int i = 0; i < dim1; ++i)
{
for (int i = 0; i < dim2; ++i)
警告:阴影的声明在这里[-Wshadow]为(int i = 0; i