V
V
Vasily Vasilyev2017-10-08 15:59:58
C++ / C#
Vasily Vasilyev, 2017-10-08 15:59:58

Why does this snippet throw an error in Visual Studio?

Situation:
I've been familiar with C++ for a couple of hours.
There is code below. In CLion it works without a hitch, everything is as it should be. The studio, on the other hand, swears like a shoemaker that the rows and columns variables should be (what the heck?) constants. Hence the question: what the hell?!

#include <iostream>
using namespace std;
int main(){
    int rows;
    int columns;
    cout << "Enter number of rows: \n";
    cin >> rows;
    cout << "Enter number of columns: \n";
    cin >> columns;
    int field[rows][columns];
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2017-10-08
@Basil_Dev

That's right. The size of an array with automatic lifetime must be known at compile time. Clion automatically includes non-standard extensions that allow VLA (similar to C99). Cure -pedantic or -pedantic-errors in CLion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question