3

I am making a Graphical Client in C with SDL library and I have a problem when I want to set my SDL_Color type.

I declare my variable as

SDL_Color color;
color = {255, 255, 255};
/* rest of code */

then gcc tells me:

25:11: error: expected expression before ‘{’ token color = {0, 0, 0};

I found pretty good answers on C++ cases with some operator overloading but I'm afraid I really don't know how to fix this one in C.

Jongware
  • 22,200
  • 8
  • 54
  • 100

3 Answers3

4

You can not assign a value to a structure like this. You can only do this to initialize your structure :

SDL_Color color = {255, 255, 255};

You can also use a designated initializer:

SDL_Color color = {.r = 255, .g = 255, .b = 255};

See the 3 ways to initialize a structure.

If you want to change the value of your structure after its declaration, you have to change values member by member:

SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;
blatinox
  • 833
  • 6
  • 18
  • What you can do though is the following: `static inline SDL_Color mk_SDL_Color(int r, int g, int b) { SDL_Color const c = {r,g,b}; return c;}` you can use it like `SDL_color color; color = mk_SDL_Color(255,255,255);`. – datenwolf Jun 13 '16 at 09:29
3

I think you can use the expression in braces only at the initialization of the variable, not in an assignment:

Initialization:

SDL_Color color = { 255, 255, 255 };  // By the way, maybe set also color.a

Assignment (member by member):

SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;
color.a = 255;

See more information about struct initialization in How to initialize a struct in accordance with C programming language standards.

Community
  • 1
  • 1
Miguel Muñoz
  • 326
  • 1
  • 6
0

Does this work in your case:?

https://wiki.libsdl.org/SDL_Color

yousafe007
  • 119
  • 1
  • 8