#define L 1
#define M L+1
int main(void){
int N=L*M*2;
char s [N]={'1','2'};
printf("%d", sizeof(s));
return 1;
}
Why does the above code cannot be compiled? The compiler of Eclipse says two things:
- Variable-sized object may not be initialized: Is because the definition of N with M and L at the macro or is it because I can’t initialize an array with type arr[x] where x is variable under any circumstances?
- excess elements in array initializer– What does it mean?
Indeed, pre-C99, the array size must be a constant expression. In C99, this has been relaxed with variable-length arrays.
(This is is nothing to do with macros.)
Fix the first problem, and this should go away.