paolovador
paolovador

Reputation: 61

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

Upvotes: 0

Views: 105

Answers (2)

paolovador
paolovador

Reputation: 61

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;

Upvotes: 0

JaredPar
JaredPar

Reputation: 754655

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;
}

Upvotes: 5

Related Questions