繁体   English   中英

结构初始化指向NULL的指针

[英]Pointer of structure initialization to NULL

我在结构内的结构上遇到问题:

typedef struct BrickStruct
{
    int type;
    SDL_Rect Brick_Coordinates;  
    SDL_Surface *Brick_Surface = NULL;  
}BrickStruct; 

我的编译器说到关于SDL_Surface结构的行:

error: expected ':', ',', ';', '}' or '__attribute__' before '=' token

但是我不太了解,因为我在我的老师面前上了关于结构指针的课,说: Coordinate *point = NULL;

坐标是内部有两个int的结构: int x,y;

有人可以向我解释那奇怪的事吗?

谢谢

C语言不允许这样内联初始化实例字段。 标准做法是编写一种工厂风格的方法来为您初始化

BrickStruct create_brick_struct()
{
  BrickStruct s;
  s.Brick_Surface = NULL;
  s.type = <default type value>;
  s.Brick_Coordinates = <default coordinatos value>;
  return s;
}

谢谢,这真的很有帮助,您让我无法承受。 由于已经使用函数来初始化坐标,因此我将同时初始化曲面。

确切地说,我的结构现在看起来像这样,对吗?:

typedef struct BrickStruct
{
    int type;
    SDL_Rect Brick_Coordinates;  
    SDL_Surface *Brick_Surface;   // I'm just wondering if I need to make it a pointer here 
}BrickStruct;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM