c malloc指向struct数组的指针

时间:2015-05-07 17:27:43

标签: c arrays pointers memory-management struct

我正在尝试创建指向console.log(JSON.stringify(friends.bill)); 结构的指针,并将其传递给array,我将在其中更改结构的值。

有人可以解释一下,为什么我的代码似乎没有为function正确分配内存,因为如果我存储两者> 27pixels ,编译版本的执行失败array中的强> integer变量,如果我只存储一个 struct变量,它适用于27像素?

integer

2 个答案:

答案 0 :(得分:2)

您将指针的地址传递给change_pixel,但它期望指针本身。它应该是:

change_pixel(pm);

我很惊讶你没有收到编译器关于错误类型参数的警告。参数声明为Pixelmap*,但您通过了Pixelmap**

答案 1 :(得分:0)

不确定是否是拼写错误,但是你分配了

sizeof(struct Pixel)

应该是

sizeof(struct Pixelmap)

,如

pm = malloc(dimx*dimy*dimz * sizeof (struct Pixelmap));