Rich
Rich

Reputation: 1851

Array Size Declaration Differences For C and C++

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?

Upvotes: 5

Views: 2875

Answers (6)

Jens Gustedt
Jens Gustedt

Reputation: 78903

As already said by others C and C++ are different regarding constant integer expressions.

But you don't have to use a macro as others suggested to have code that works with both. If you want to do it with "scope" you can do it with an enumeration constant. Something like

enum { num = 16 };
struct inputs {
  double X1[num];
  double X2[num];
};

will work for both, regardless if you put this in file or function scope.

Upvotes: 0

Morten Kristensen
Morten Kristensen

Reputation: 7613

You have to use a constant value. So in C you have to use a #define in this regard. For C++ it would have been fine.

Use this instead:

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

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490078

Yes, there's a difference. In C, a const variable still isn't treated as a true compile-time constant (officially, it's not allowed a part of a constant 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 const variable 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:

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

This is allowed in C, but not in C++. This is a variably modified array; the error message you're getting is basically trying to tell you that these are only allowed inside functions.

Upvotes: 3

jpalecek
jpalecek

Reputation: 47762

In C++, compile time constants can be used as lengths in array declarations and const variables (only some of them) can be compile-time constants. This is why it will work in C++.

C, however, has a feature that looks similar, the variable-length array. It means you can use any integer expression (even runtime computed) as array length, but only for local variables. This is why your compiler complains about "variably modified ‘X1’ at file scope".

Upvotes: 0

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143051

Not in struct. You can do it in local scope.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182619

I can point you to a C FAQ: I don't understand why I can't use const values in initializers and array dimensions.

What it basically says is that num isn't a true constant, it's just read-only. To get a true constant you would need a #define num 16.

Also on that page: C is unlike C++ in this regard.

Upvotes: 8

Related Questions