访问结构中结构中的指针

时间:2013-02-17 08:53:51

标签: c pointers struct

引发错误,我不确定原因:

  

physics.c:74:错误:初始化中不兼容的类型

typedef struct gameBody gameBody;

struct gameBody
{
    cpBody *body;
    int numberOfShapes;
    cpShape *arrayOfShapes; //This stores an array of pointers to Shapes
};

//Struct that stores the cpSpace object and the array of pointers to the body objects
typedef struct gameSpace gameSpace;

struct gameSpace
{ 
    cpSpace *space;
    int numberOfObjects;
    gameBody *arrayOfObjects;       //This stores an array of gameBodys
};


for(int i = 0; i < space -> numberOfObjects; i++)
{
    //body info
    gameBody *gBody = space -> arrayOfObjects[i];
}

4 个答案:

答案 0 :(得分:1)

我猜它在行

gameBody *gBody = space -> arrayOfObjects[i];

虽然space->arrayOfObject是指针,但space->arrayOfObject[i] 不是指针。

您必须声明gBody不是指针:

gameBody gBody = space->arrayOfObjects[i];

或者在数组中的对象上使用address-of运算符;

gameBody *gBody = &space->arrayOfObjects[i];

答案 1 :(得分:1)

 gameBody *gBody = &(space -> arrayOfObjects[i]);

gameBody *gBody = space -> arrayOfObjects + i;

答案 2 :(得分:0)

gBody的类型为gameBody *space->arrayOfObjects[i]的类型为gamebody。也许你想要:

gameBody *gBody = &space->arrayOfObjects[i];

答案 3 :(得分:0)

来自您发布的代码

gameBody *gBody = space->arrayOfObjects[i]&lt;〜是问题......

应该是:

gameBody *gBody = &(space->arrayOfObjects[i]);