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)代码>谢谢大家的支持。真的很感激!