0

I'm trying to understand the difference that the scoping makes:

//global scope
int size = 4;
int array[size]; // error: variably modified 'array' at file scope

int main(void) {
  int buff[size]; // works!
}

how does using a variable as array size doesn't work globally but works inside main? It would work if I use a macro instead.

Also, does using const matter for size?

xyf
  • 664
  • 1
  • 6
  • 16

1 Answers1

0

simply make it compile time integral constant expression, since array length must be specified at the compile time, with define

#define SIZE   6

int array[SIZE];
Adam
  • 2,820
  • 1
  • 13
  • 33