-2

I get the compile error: "expression must have a constant value", even though size is a constant. where is my fault ı can't find it??

 void foo(int n)
    {
    int x[n];
    int i = 5;
    if (i == 0)
    i--;
    x[i] = 5; //OK, since i is 4
    }

1 Answers1

0

C89 doesn't have variable length arrays, so

this line is wrong:

int x[n];

It depends on the function parameter n which is not a compile-time constant.

You could replace the raw array with a malloc:

int* x = malloc( n * sizeof( int ) );
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87