Difference in array sizes Differences for C and C ++

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?

+5
source share
6 answers

, . C a const - ( constant expression), . , , , C , , , , .

++ const , .

, , , :

void f(int size) { 
    int array[size];
}

C, ++. variably modified; , , , .

+3

C FAQ: , const .

, num , . , #define num 16.

: C ++ .

+8

. .

0

. , C #define . ++ .

:

#define num 16
struct inputs{
  double X1[num];
  double X2[num];
};
0

++ , const ( ) . ++.

C, , , , . , ( ) , . "" X1 ".

0

, 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.

0
source

All Articles