gpt4 book ai didi

c - 如何正确访问此结构中的指针?

转载 作者:太空宇宙 更新时间:2023-11-04 02:58:22 25 4
gpt4 key购买 nike

下面是 2 个结构的定义,然后是使用它们的简短方法体。我不明白为什么编译器会抛出错误:

physics.c:95: error: incompatible types in assignment

cpBodycpSpace 是来自外部库的类型,这不是问题的一部分。

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
};

void physicsAddBody(gameSpace *space, gameBody *body, int objectIndex)
{
gameBody *array = space -> arrayOfObjects;
array[objectIndex] = body; //THIS IS WHERE THE ERROR IS THROWN
}

最佳答案

array[objectIndex] = body;

左边是gameBody,右边是gameBody*

您是尝试复制结构还是将指针分配给指针数组?

关于c - 如何正确访问此结构中的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14919300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com