This sketch does not compile in the Arduino IDE
void setup() {
// put your setup code here, to run once:
}
struct test {
int i;
char variable[];
};
typedef struct test test;
test t = {
0, "hi"
};
void loop() {
// put your main code here, to run repeatedly:
}
Arduino throws
sketch_may09a:16: error: initializer-string for array of chars is too long [-fpermissive]
};
^
exit status 1
initializer-string for array of chars is too long [-fpermissive]
However compiling with g++ works just fine. How can I fix it? Or is there a principle reason why flexible array members are not supported?
[]
array as the last member of astruct
. However, in standard C language the only way to make such structs to "have different lengths" is tomalloc
them individually. This is what this[]
feature is designed for. In all other contexts the[]
array simply "disappears" (i.e. it is an array of size 0). So, the issue simply does not exist in situations when one'd try to create an array of such structs.