0

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

1
  • 4
    You can't initialize a struct's member in the struct's declaration. Commented Mar 7, 2014 at 19:17

2 Answers 2

5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.