Consider the piece of declaration:
struct my_cool_struc {
int field_x;
int field_Y;
int field_z;
};
#define MY_COOL_MACRO(x,y,z) \
{ \
.field_x = (x), \
.field_y = (y), \
.field_z = (z), \
}
static const struct my_cool_struc why[] = {
MY_COOL_MACRO(1,2,3),
MY_COOL_MACRO(6,5,4),
MY_COOL_MACRO(7,8,9),
{}
};
static int my_cool_func(...)
{
struct my_cool_struc p[10];
int a1, a2, a3;
unsigned int index = 0;
...
p[index++] = MY_COOL_MACRO(a1, a2, a3);
...
return 0;
}
While in the why
assignment everything works fine, compiler can's build the function, fails on syntax parser.
The following fixes the issue:
- { \
+ (struct my_cool_struc) { \
or
- p[index++] = MY_COOL_MACRO(a1, a2, a3);
+ p[index++] = (struct my_cool_struc)MY_COOL_MACRO(a1, a2, a3);
Compiler is GCC (different versions) on Linux.