如何访问这个指针正确结构体? [英] How do I access the pointer in this struct correctly?
本文介绍了如何访问这个指针正确结构体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是2结构,然后使用它们很短的方法体的定义。
我不明白为什么编译器会引发错误:
physics.c:95:错误:不兼容的类型分配
块引用>
cpBody
和cpSpace
从外部库,这不是问题的一部分类型。typedef结构gameBody gameBody;结构gameBody
{
cpBody *体;
诠释numberOfShapes;
cpShape * arrayOfShapes; //这个存储指针数组形状
};//结构体,它存储cpSpace对象和指针数组到主体对象
typedef结构游戏空间游戏空间;游戏空间结构
{
cpSpace *空间;
诠释numberOfObjects;
gameBody * arrayOfObjects; //这个存储gameBodys数组
};无效physicsAddBody(*游戏空间空间,gameBody *身体,诠释objectIndex)
{
gameBody *阵列=空间 - > arrayOfObjects;
数组[objectIndex] =身体; //这就是引发错误
}
解决方案
数组[objectIndex] =身体;
块引用>在左侧是一个
gameBody
,右侧一个gameBody *
。您尝试复制结构或做你想做的指针分配到一个指针数组?
Below are definitions for 2 structs, then a short method body that uses them. I don't understand why the compiler throws the error:
physics.c:95: error: incompatible types in assignment
cpBody
andcpSpace
are types from an external library, which isn't part of the problem.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;
On the left side is a
gameBody
, on the right side agameBody*
.Do you try to copy the struct or do you want to assign the pointer into a pointer-array?
这篇关于如何访问这个指针正确结构体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文