const int num = 16;
struct inputs{
double X1[num];
double X2[num];
};
Gives me an error:
error: variably modified ‘X1’ at file scope
The same was true for ‘X2’.
But I remember the above is fine for C++, the above is fine (I may be mistaken for C++).
Can anybody clarify this for me?
Yes, there’s a difference. In C, a
constvariable still isn’t treated as a true compile-time constant (officially, it’s not allowed a part of aconstant expression), so this isn’t allowed. Note, however, that although C doesn’t require that the compiler allow it, the standard does give permission for an implementation to accept other forms of constant expressions, so it’s free to accept it if it chooses.In C++, a
constvariable is treated as a constant, so it is allowed.Interestingly, roughly the reverse is true when you use a value passed as a function parameter:
This is allowed in C, but not in C++. This is a
variably modifiedarray; the error message you’re getting is basically trying to tell you that these are only allowed inside functions.