简体   繁体   中英

C array of structs syntax error

Here is the piece of code that I am getting error, SDL_Rect's definition is copied over from documentation:

typedef struct{
  Sint16 x, y;
  Uint16 w, h;
} SDL_Rect;


SDL_Rect clips[4];

clips[0].x = 0;
clips[0].y = 0;
clips[0].w = 100;
clips[0].h = 100;

Here is how I am compiling it:

gcc -march=native -static-libgcc -o sprite sprite.c functions.o -L/usr/lib -lSDL -lpthread -lm -ldl -lpthread -lSDL_image

Here is the error that I am getting: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token expected '=', ',', ';', 'asm' or '__attribute__' before '.' token for each line of clips[..] . I have tried putting paranthesis around clips[..] but It didn't work either. This is plain "C" by the way. Not C++.

Edit

I have copied over SDL_Rect here from SDL's documentation in order to show what it was. It is not actually in the source file I am using. Therefore, missing of the semicolon cannot be the issue. And this code is in the global scope.

This (-initialisation-) assignment is at global scope which is not possible.

It is possible to initialise the array:

SDL_Rect clips[4] = {
                        { 0, 0, 100, 100 }, /* Element 0 initial values */
                        { 1, 1, 200, 200 }  /* Element 1 initial values */
                                            /* Element 2 and 3 unspecified so
                                               zero initialised. */
                    };

If C99 compliant compiler you can explicitly state the variables being initialised:

SDL_Rect clips[4] = {
                        { .x = 0, .y = 0, .w = 100, .h = 100 }
                    };

Missing a ; after the SDL_Rect:

typedef struct{ Sint16 x, y; Uint16 w, h; } SDL_Rect;

You seem to be missing a ; after SDL_Rect in your struct definition.

It should be:

typedef struct{
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;

Notice the semicolon at the end.

There miss the semicolon at the end of the structure:

typedef struct{
  Sint16 x, y;
  Uint16 w, h;
} SDL_Rect;

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