const int num = 16; struct inputs{ double X1[num]; double X2[num]; };
Gives me an error:
error: modified variable "X1 in the file area
The same is true for "X2".
But I remember that this is good for C ++, this is good (I may be wrong for C ++).
Can anyone clarify this for me?
, . C a const - ( constant expression), . , , , C , , , , .
const
constant expression
++ const , .
, , , :
void f(int size) { int array[size]; }
C, ++. variably modified; , , , .
variably modified
C FAQ: , const .
, num , . , #define num 16.
num
#define num 16
: C ++ .
. .
. , C #define . ++ .
#define
:
#define num 16 struct inputs{ double X1[num]; double X2[num]; };
++ , const ( ) . ++.
C, , , , . , ( ) , . "" X1 ".
, C ++ .
But you do not need to use a macro, as others suggested having code that works with both. If you want to do this with scope, you can do it with an enum constant. Sort of
enum { num = 16 }; struct inputs { double X1[num]; double X2[num]; };
will work for both, regardless of whether you put it in the files or functions area.