尝试使用 malloc 做 3 维矩阵

Tried to do 3 dimensional matrix using malloc

我想使用 malloc 收集 3 维矩阵。还有矩阵打印部分,但我没有包括。

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i,j,k,m,n,o;
   float ***A;
   printf("Input dimension for matrix(m,n,o)\n");
   printf("Enter m : ");
   scanf("%d",&m);
   printf("Enter n : ");
   scanf("%d",&n);
   printf("Enter o : ");
   scanf("%d",&o);
   A = (float***)malloc(o*sizeof(float**));
   for(i=0;i<o;i++){
           A[i] = (float**)malloc(m*sizeof(float*));
           for(j=0;j<m;j++){
               A[i][j] = (float*)malloc(n*sizeof(float));
           }
   }
   for(i=0;i<o;i++){
       for(j=0;j<m;j++){
           for(k<0;k<n;k++){
               printf("Input number for (%d,%d,%d) : ",j+1,k+1,i+1);
               scanf("%f",&A[i][j][k]);
           }
       }
   }
   return 0;
}

程序似乎在我输入完m,n,o 后跳到最后,所以我无法输入矩阵的任何值。我必须使用 malloc 因为它是任务所必需的。

编辑:一切都是错误的。 m代表行,n代表列,o代表多个mXn的矩阵。

使用动态可变长度数组:

float (*A)[n][m] = calloc(o, sizeof *A);

最后用

释放它
free(A);

还有一个必须传递扫描对象的地址,而不是它的值。应该是:

scanf("%f", &A[i][j][k])

这应该有效:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    float ***A;

    // n1, n2 and n3 will contain the three dimensions (i.e.: A[n1][n2][n3])
    size_t n1, n2, n3;

    printf("Insert first dimension: ");
    if (scanf("%zu", &n1) != 1) {
        printf("ERROR: cannot read number\n");
        return 1;
    }

    printf("Insert second dimension: ");
    if (scanf("%zu", &n2) != 1) {
        printf("ERROR: cannot read number\n");
        return 1;
    }

    printf("Insert third dimension: ");
    if (scanf("%zu", &n3) != 1) {
        printf("ERROR: cannot read number\n");
        return 1;
    }

    // firstly, here we alloc space for first dimension
    A = malloc(n1 * sizeof(float**));

    for (size_t i = 0; i < n1; i += 1) {
        // now we alloc space for second dimension
        A[i] = malloc(n2 * sizeof(float*));
        for (size_t j = 0; j < n2; j += 1) {
            // here we alloc space for third dimension
            A[i][j] = malloc(n3 * sizeof(float));
        }
    }

    // let's ask the user to input all elements into the array
    for (size_t i = 0; i < n1; i += 1) {
        for (size_t j = 0; j < n2; j += 1) {
            for (size_t k = 0; k < n3; k += 1) {
                printf("Insert value for A[%zu][%zu][%zu]: ", i, j, k);
                if (scanf("%f", &(A[i][j][k])) != 1) {
                    printf("ERROR: cannot read number\n");
                    return 1;
                }
            }
        }
    }

    // now let's print all the numbers that the user has input previously
    for (size_t i = 0; i < n1; i += 1) {
        for (size_t j = 0; j < n2; j += 1) {
            for (size_t k = 0; k < n3; k += 1) {
                printf("A[%zu][%zu][%zu] = %f\n", i, j, k, A[i][j][k]);
            }
        }
    }

    return 0;
}

我没有编译过,不过应该是正确的。仔细看看分配是如何进行的。

注意:当然,完全正确(例如:使用 free() 和避免使用 scanf())已被故意避免。

P.S.: 正如其他用户已经提到的,注意为您的编译器启用警告(例如:GCC 上的 -Wall -Wextra),因为它会为您简化调试。

我认为您所做的一切都是在获取输入时在 for 循环中搞砸了。根据上下文,3D 矩阵,我调试了代码并提出了一个有意义的版本。

我会一一指出你犯的错误

首先,scanf 获取变量的内存地址,而不是它的解除引用版本。

因此,将其从 scanf("%f",A[i][j][k]); 更改为 scanf("%f",&(A[i][j][k]));。这个也可以 scanf("%f",A[i][j] + k);.

这一行中,for(k<0;k<n;k++)k甚至都没有初始化。所以你基本上是在垃圾价值上运作。由于我已经对 c pointer hell 的破译感到困惑,我什至无法处理最终结果。更有意义的代码是 for(k=0;k<n;k++).

之后,您切换了嵌套 for 循环的控制逻辑。

将这段代码for(j=0;j<m;j++)中的变量m替换为nfor(j=0;j<n;j++)

在下一个循环中反之亦然。把这个for(k=0;k<n;k++)修改成这个for(k=0;k<m;k++).

最终代码如下所示,

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i,j,k,m,n,o;
   float ***A;
   printf("Input dimension for matrix(m,n,o)\n");
   printf("Enter m : ");
   scanf("%d",&m);
   printf("Enter n : ");
   scanf("%d",&n);
   printf("Enter o : ");
   scanf("%d",&o);
   A = (float***)malloc(o*sizeof(float**));
   for(i=0;i<o;i++){
           A[i] = (float**)malloc(n*sizeof(float*));
           for(j=0;j<n;j++){
               A[i][j] = (float*)malloc(m*sizeof(float));
           }
   }
   for(i=0;i<o;i++){
       for(j=0;j<n;j++){
           for(k=0;k<m;k++){
               printf("Input number for (%d,%d,%d) : ", i + 1, j + 1, k + 1);
               scanf("%f",&(A[i][j][k]));
           }
       }
   }
   
   
   return 0;
}

它现在可以用作 3D 矩阵。

为了理解代码,我不得不画了一张乱七八糟的图。我附上那个。可能会有一些帮助。

由于您已经省略了一些代码,希望您没有忘记使用 free 来防止内存泄漏。