[英]How to accsses a struct member of a struct that was setted as a pointer?
I am trying to create a submarine game in c so i made a sturct that will connect the "location" of the submarine on the Board to the submarine it self, these are the structs i use; 我试图在C中创建一个潜水艇游戏,所以我做出了一个坚决的决定,将董事会中潜水艇的“位置”与其自身的潜水艇连接起来,这些是我使用的结构;
struct Point_s
{
int x;
int y;
};
struct BoardPart_s
{
boolean ishit;//yes = 1 no = 0
Submarine* Sub;
};
struct Submarine_s
{
Point start;
Point end;
int Life;// this is calculated from the diffrence of the non equal part of the x/y Point
};
now when the assigment was stated like this 现在,当这样的陈述
Board[xstart][yend]->Sub=&(Plist->sub);// plist is a linked list conatining a submarine
however when i try to accses its member life like that 但是当我试图像这样强调它的成员生活时
BoardPart BoardP2[BoardSize][BoardSize];// boardsize is 10
.
.
.
.
.
if( BoardP2[x][y]->Sub->Life <= 0)
i get a compiler error "request for member 'Life' in something not a structure or union" 我收到一个编译器错误“在结构或联合中请求成员'Life'”
i should note that the typedef that are in the header are stated like this 我应该注意,标头中的typedef像这样声明
typedef struct Point_s* Point;
typedef struct Submarine_s* Submarine;
typedef struct Command_s* Command;
typedef struct BoardPart_s* BoardPart;
In your example the Sub
field of BoardPart_s
struct is Submarine *
, and Submarine
is Submarine_s *
. 在您的示例中,
BoardPart_s
结构的Sub
字段是Submarine *
,而Submarine
是Submarine_s *
。 So to access the Life
field of that Sub
you should write (**(BoardP2[x][y]->Sub)).Life
. 因此,要访问该
Sub
的Life
字段,您应该编写(**(BoardP2[x][y]->Sub)).Life
。
To avoid this kind of confusion you should always avoid typedefing to pointers to types. 为了避免这种混乱,您应该始终避免对类型指针的类型定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.