Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 对四维立方体使用指针_C_Pointers - Fatal编程技术网

C 对四维立方体使用指针

C 对四维立方体使用指针,c,pointers,C,Pointers,我正在尝试使用***指针打印4D立方体 #include <stdio.h> #include <stdlib.h> #define DIM 4 void printCube(char ****cube, int dim) { int x, y, z; for (z = 0; z < dim; z++) { for (y = 0; y < dim; y++) { for (x = 0; x <

我正在尝试使用
***
指针打印4D立方体

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

#define DIM 4

void printCube(char ****cube, int dim) {
    int x, y, z;
    for (z = 0; z < dim; z++) {
        for (y = 0; y < dim; y++) {
            for (x = 0; x < dim; x++) {
                printf("%c ", *cube[z][y][x]);
            }
            printf("\n");
        }
        printf("------------------------------------\n");
    }
}

int main() {
    char ***cube = (char ***)malloc(sizeof(char **) * DIM);
    int x, y, z;
    for (z = 0; z < DIM; z++) {
        cube[z] = (char **)malloc(sizeof(char **) * DIM);
        for (y = 0; y < DIM; y++) {
            cube[z][y] = (char *)malloc(sizeof(char *) * DIM);
            for (x = 0; x < DIM; x++) {
                cube[z][y][x] = ((x + y + z) % 26) + 'A';
            }
        }
    }

    printCube(&cube, DIM);

    for (z = 0; z < DIM; z++) {
        for (y = 0; y < DIM; y++) {
            for (x = 0; x < DIM; x++) {
                free(cube[z][y][x]);
            }
            free(cube[z][y]);
        }
        free(cube[z]);
    }
    free(cube);
    return 0;
}
有人告诉我如何更正此代码吗


非常感谢您的支持。

在将
多维数据集
传递给
打印多维数据集
时,您不需要使用该多维数据集的地址。相反,只需直接传递它,在
printCube
中声明它为
char***
,然后在
printCube
中作为
cube[z][y][x]
访问它


按照现在的方式,您在
printCube
中使用
*cube[z][y][x]
,这不起作用,因为
*
绑定比
[]
更松散。如果您将其更改为
(*cube)[z][y][x]
,它将起作用。但正如我所说,首先不需要额外的间接寻址级别。

代码中存在多个问题:

  • 您的多维数据集只有3维,它是一个普通的多维数据集,表示为指向
    int
    数组的指针数组。将此数组直接传递给
    printCube
    函数实际上会传递指向其第一个元素的指针,类型为
    int***
    。在您的例子中,由于已分配顶级数组,因此您已经有了一个类型为
    int***
    的指针。因此,函数原型应该是
    void printCube(char***cube,int dim)
    。没有理由传递指针的地址而不是其值
  • 您在计算分配大小时有一些错误:
    cube[z]=(char**)malloc(sizeof(char**)*DIM)应该是

    cube[z] = (char **)malloc(sizeof(char *) * DIM);
    
    cube[z][y] = (char *)malloc(sizeof(char) * DIM);
    
    cube[z][y]=(char*)malloc(sizeof(char*)*DIM)应该是

    cube[z] = (char **)malloc(sizeof(char *) * DIM);
    
    cube[z][y] = (char *)malloc(sizeof(char) * DIM);
    
  • 您应该删除释放循环中的一个级别,因为
    cube[z][y][x]
    不是分配的指针,而只是
    cube[z][y]
    指向的数组中的一个字符。遗憾的是,您的编译器只接受了一个简单的警告代码。您应该使用
    gcc-Wall-Werror
    来避免此类愚蠢的错误

以下是更正的版本:

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

#define DIM 4

void printCube(char ***cube, int dim) {
    int x, y, z;
    for (z = 0; z < dim; z++) {
        for (y = 0; y < dim; y++) {
            for (x = 0; x < dim; x++) {
                printf("%c ", cube[z][y][x]);
            }
            printf("\n");
        }
        printf("------------------------------------\n");
    }
}

int main() {
    char ***cube = (char ***)malloc(sizeof(char **) * DIM);
    int x, y, z;
    for (z = 0; z < DIM; z++) {
        cube[z] = (char **)malloc(sizeof(char *) * DIM);
        for (y = 0; y < DIM; y++) {
            cube[z][y] = (char *)malloc(sizeof(char ) * DIM);
            for (x = 0; x < DIM; x++) {
                cube[z][y][x] = ((x + y + z) % 26) + 'A';
            }
        }
    }

    printCube(cube, DIM);

    for (z = 0; z < DIM; z++) {
        for (y = 0; y < DIM; y++) {
            free(cube[z][y]);
        }
        free(cube[z]);
    }
    free(cube);

    return 0;
}

您是运算符优先级的牺牲品。在C语言中,后固定运算符的优先级高于前固定运算符。在您的例子中,前缀运算符是
“*”
,后缀运算符是
“[]”

因此,表达:

*cube[z][y][x]
相当于:

*(cube[z][y][x])
这是不可取的,因为
cube[z][y][x]
是指向无效内存的指针,尝试使用此指针将导致未定义的行为

应首先取消引用
多维数据集
指针,然后使用下标运算符:

(*cube)[z][y][x]

这就解决了你的问题。另一方面,由于您不试图修改函数中
cube
指针的值,而只是使用它的值,因此可以简化函数以获取
char***
。然后按值传递
多维数据集
指针,不必考虑运算符优先级。

它会引发什么错误?想法:简化为
多维数据集=(char***)malloc(sizeof(char**)*DIM)-->
cube=malloc(sizeof*cube*DIM)。同样:
cube[z]=malloc(sizeof*size[z]*DIM)。。。。立方体[z][y]=malloc(大小*立方体[z][y]*DIM)谢谢大家的支持。真的很感激!