簡體   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