取消引用未定义的指针值?(Dereference of undefined pointer value?)

编程入门 行业动态 更新时间:2024-10-08 22:12:33
取消引用未定义的指针值?(Dereference of undefined pointer value?)

我正在尝试malloc一个3d数组然后初始化它,如下所示,问题是,我得到一个警告说我正在尝试'取消引用下一行中的未定义指针值': Parque->matriz[i][j][p]='@';

任何帮助将不胜感激。

我的代码如下:

parque *Parque; Parque = (parque *) malloc(sizeof(parque)); Parque->matriz = (char***)malloc(x * sizeof(char **)); for (i = 0; i < x; i++) { Parque->matriz[i] = (char**)malloc(y * sizeof(char*)); for (j = 0; j < y; j++) { Parque->matriz[i][j] = (char*)malloc(z*sizeof(char)); } } for (p=0; p<z; p++) { for (j=y-1;j>=0; j--) { for (i=0; i<x; i++) { Parque->matriz[i][j][p]='@'; } } }

这是struct parque的定义:

struct _parque{ int dimx; int dimy; int pisos; int entradas; int acessos; int nodes; char ***matriz; int capacidade; int lugares_ocupados; }; typedef struct _parque parque;

I'm trying to malloc a 3d array and then initialise it, as follows, the problem is, I get a warning saying I'm trying to 'dereference an undefined pointer value' in the following line: Parque->matriz[i][j][p]='@';

Any help will be greatly appreciated.

My code is as follows:

parque *Parque; Parque = (parque *) malloc(sizeof(parque)); Parque->matriz = (char***)malloc(x * sizeof(char **)); for (i = 0; i < x; i++) { Parque->matriz[i] = (char**)malloc(y * sizeof(char*)); for (j = 0; j < y; j++) { Parque->matriz[i][j] = (char*)malloc(z*sizeof(char)); } } for (p=0; p<z; p++) { for (j=y-1;j>=0; j--) { for (i=0; i<x; i++) { Parque->matriz[i][j][p]='@'; } } }

and this is the definition of struct parque:

struct _parque{ int dimx; int dimy; int pisos; int entradas; int acessos; int nodes; char ***matriz; int capacidade; int lugares_ocupados; }; typedef struct _parque parque;

最满意答案

你的代码似乎是正确的,尽管你有一种有趣的方法来迭代索引。 也许你的编译器感到困惑。 请尝试:

for (i = 0; i < x; i++) { Parque->matriz[i] = (char**)malloc(y * sizeof(char*)); for (j = 0; j < y; j++) { Parque->matriz[i][j] = (char*)malloc(z*sizeof(char)); for (p = 0; p < z; p++) { Parque->matriz[i][j][p]='@'; } } }

这在语义上等同于第二组嵌套循环。

Your code seems correct, though you have a funny way of iterating over the indices. Maybe your compiler got confused. Please try:

for (i = 0; i < x; i++) { Parque->matriz[i] = (char**)malloc(y * sizeof(char*)); for (j = 0; j < y; j++) { Parque->matriz[i][j] = (char*)malloc(z*sizeof(char)); for (p = 0; p < z; p++) { Parque->matriz[i][j][p]='@'; } } }

This would be semantically equivalent to you second set of nested loops.

更多推荐

本文发布于:2023-08-07 17:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   未定义   Dereference   undefined   pointer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!