-1
int n=5;
int arr[n];

I want to declare size of array as above in C++, but I get error while compiling. I find a lot of code in internet which uses these type of declaration instead of simple putting int arr[5]. How come the code compiles successfully for them but not for me. P.S: I use windows7 and Visual Studio(IDE).

Error Message : Expresion must have a constant value

Unbreakable
  • 7,776
  • 24
  • 90
  • 171

2 Answers2

3

The number of elements of the array, the array bound, must be a constant expression.

You have to use

const int n = 5;

or

constexpr int n = 5;

else it is a non standard extension : variable length array (VLA).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • It worked for me now. Can you explain in more layman terms why it did not compile earlier when I used just `int` instead of `const int`. – Unbreakable Dec 29 '14 at 14:01
  • Does it have to do something with the Visual Studio? – Unbreakable Dec 29 '14 at 14:04
  • Better yet, don’t use a C array. – Konrad Rudolph Dec 29 '14 at 14:05
  • With `int`, you don't have a constant expression. Some compilers may provide extension to allow your code but then your code is not portable and won't compile for all compiler. – Jarod42 Dec 29 '14 at 14:07
  • @Jarod42 I totally got your point. Can you please explain once how VLA differs from Vector. Because now I am thinking Vector is also kind of same as VLA. Because once the Size starts getting equal to capacity we can let user input the new size using VLA concept. Am I making sense or getting confused unnecessarily. – Unbreakable Dec 29 '14 at 14:14
  • 1
    @Unbreakable: VLA is a fixed size array (with size known at runtime), `vector` is a dynamic resizeable array. – Jarod42 Dec 29 '14 at 15:12
3

The error message actually describes rather well what’s going on: C++ does not support arrays with a non-constant size (more precisely, the size needs to be known at compile time).

There are two solutions for this:

  1. If the size is actually a constant, declare it as constexpr (if you can’t use C++11, you can also use const):

    constexpr int n = 5;
    std::array<int, n> arr;
    

    Which requires the standard header <array>. Or, if you cannot use C++11, change the second line to

    int arr[n];
    
  2. If the size isn’t known at compile time, don’t use a static array, use a dynamic container instead:

    int n = 5;
    std::vector<int> arr(n);
    

    This requires the <vector> standard header.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    And `std::array` requires `` standard header. – Jarod42 Dec 29 '14 at 14:12
  • I got your point. But I am getting confused now with VLA and Vectors. Do they kind of related to each other. Because on both of them we can alter capacity of array at RUN TIME. – Unbreakable Dec 29 '14 at 14:17
  • 1
    @Unbreakable VLAs and vectors are fundamentally different: VLAs are stored on the stack, which is not usually a good idea (the stack size is severely limited, and if you don’t control the size of the array at compile time it could be too large). Vectors by contrast store the actual data on the heap, where size is a *much* smaller concern. In addition, VLAs are simply not valid C++ at the moment, and not all compilers support it. Future versions of C++ will however support VLAs. – Konrad Rudolph Dec 29 '14 at 14:24