0
char filename[256];
scanf("%s", filename);
FILE *file = fopen(filename, "r");

int M, N, C, i, j, P, G;

fscanf(file, "%d", &M); 
fscanf(file, "%d", &N); 
fscanf(file, "%d", &C);

int xv[M + 1], yv[M + 1], h[M + 1]; // line 21

for (i = 1; i <= M; i++) {
    fscanf(file, "%d", &xv[i]);
    fscanf(file, "%d", &yv[i]);
    fscanf(file, "%d", &h[i]);
}

Compiler VC 2017 return error:

error C2131: expression did not evaluate to a constant

note: fault caused by a reading of a variable out of its lifetime

note: see use of "%*"

Why this error appears in VS 2017 and when I use the Dev-C++ IDE it does not give an error and it executes correctly?

Community
  • 1
  • 1
Cava
  • 5,346
  • 4
  • 25
  • 41
  • 2
    Instead of `int xv[M + 1], yv[M + 1], h[M + 1];` you should use `std::vector xv(M + 1), yv(M + 1), h(M + 1);`. – Henri Menke Nov 12 '18 at 00:37
  • 2
    *when I use the Dev-C++ IDE it does not give an error* -- Yet another victim of the `g++` variable-length array non-C++ syntax. VS 2017 is correct in giving you the error. That line of code is not C++. – PaulMcKenzie Nov 12 '18 at 00:42
  • 1
    It works in Dev-C++ because Dev-C++ uses MinGW, and MinGW uses gcc, and gcc [supports variable-length arrays as a nonstandard extension](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). – Raymond Chen Nov 12 '18 at 00:42

0 Answers0