0

I'm trying to create an array and the size of the array depends on the user input. But there is an error in my code, It said: "expression must have a constant value".

Here is my code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int var;
    cout << "What size do you want to choose: "<< endl;
    cin >> var;

    int arr[var];
}

How can I change my code and make it work?

  • 2
    C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array), the size of the array must be a compile-time constant. Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude Feb 04 '20 at 06:53
  • Use a `std::vector`. You cannot have arrays with non-constant lengths in C++. – Tanveer Badar Feb 04 '20 at 06:54
  • https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard Related question. – Jan Schultke Feb 04 '20 at 06:57
  • 1
    @Logica Your proposed edit adds three tags, two of which are unhelpful. Tags should help finding questions. They should indicate what a question is about, not what it happens to mention. That is not good enough in my opinion for an edit which gets you reputation. It might still be accepted, but please consider this in further edit proposals. – Yunnosch Feb 04 '20 at 07:04

4 Answers4

4

How can I change my code and make it work?

Change

int arr[var];

to

std::vector<int> arr(var);

This will require #include <vector>

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • It's also possible to use `std::unique_ptr arr = std::make_unique(size);` as long as one doesn't need to access the size again. – Jan Schultke Feb 04 '20 at 06:56
  • @J.Schultke note that `std::unique_ptr` requires C++11, and `std::make_unique(size)` requires C++14. – Remy Lebeau Feb 04 '20 at 07:03
  • In addition, `std::vector` provides significantly more bang- for-the-buck that a `unique_ptr`-wrapped array. `vector` can be copied, for one thing. Occasionally a `unique_ptr`-wrapped array is the better tool for the job, but it's pretty rare. – user4581301 Feb 04 '20 at 07:56
3

The syntax you are using is known as a "variable length array", and is NON-STANDARD. Only a few compilers support it as an vendor-specific extension to the C++ language.

A standard compliant fixed-length array must have its size known to the compiler as a compile-time constant.

For what you want, you need to use the new[] operator instead:

int *arr = new int[var];
...
delete[] arr;

Or better, use a std::vector container:

#include <vector>

std::vector<int> arr(var);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

To allocate an array dynamically, we use the array form of new and delete (often called new[] and delete[]):

#include <iostream>

int main()
{
    std::cout << "Enter a positive integer: ";
    int length{};
    std::cin >> length;

    int *array{ new int[length]{} }; // use array new.  Note that length does not need to be constant!

    std::cout << "I just allocated an array of integers of length " << length << '\n';

    array[0] = 5; // set element 0 to value 5

    delete[] array; // use array delete to deallocate array

    // we don't need to set array to nullptr/0 here because it's going to go out of scope immediately after this anyway

    return 0;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • You should be using `auto array = std::make_unique(length);`, this way you don't need to delete the array manually yourself. – Jan Schultke Feb 04 '20 at 06:59
0

Arrays in C++ must have constant size known at compile-time. You can preallocate several constant sizes known at compile time and offer the user to choose one of them. Alternatively, you can use another data structure that allows dynamic size allocation e.g std::vector. Good luck!

aviad
  • 8,229
  • 9
  • 50
  • 98