简体   繁体   中英

Pointer of structure initialization to NULL

I'm having a problem with a structure within a structure:

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

my compiler says that about the line with the SDL_Surface structure:

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

But I don't really understand because I got in front of me my teacher's lesson about pointer of structure saying that: Coordinate *point = NULL;

Coordinate being a structure with two int inside: int x,y;

Can somebody explain me that weird thing ?

Thanks

The C language does not allow for the initialization of instance fields inline like this. The standard practice is to write a factory style method which does the initialization for you

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

thanks, that's been really helpful and you took a weight off my mind. Since I already use a function to initialize my coordinates, I will initialize my surfaces at the same time.

To be real really clear: my structure will now looks like this, right ?:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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