取消引用未定义的指针值?(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.

取消引用未定义的指针值?(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.