Initializing a structure with a triple operator

Why can't the ternary operator initialize a structure type while it can be used to initialize a base type such as int?

Code example:

#include <stdio.h> #define ODD 1 int main(int argc, const char *argv[]) { static struct pair_str { int first; int second; } pair = ( ODD ) ? {1,3} : {2,4}; // ERROR printf("pair %d %d\n", pair.first, pair.second); int number = (ODD) ? 1 :2; // FINE return 0; 

}

Compiler Errors:

 /home/giuseppe/struct.c: In function 'main': /home/giuseppe/struct.c:12:23: error: expected expression before '{' token /home/giuseppe/struct.c:12:29: error: expected expression before ':' token 
+4
source share
1 answer

Of course, use the C99 literary examples:

 pair = odd ? (struct pair_str){ 1, 3 } : (struct pair_str){ 2, 4 }; 
+10
source

Source: https://habr.com/ru/post/1493796/


All Articles