0
int n;
printf("How many?");
scanf("%d", &n);

int array[n];

These are the errors I get..
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0 error C2133: 'array': unknown size

Any help would be appreciated.

2 Answers2

0

Some compilers don't support variable-length array and unfortunately your compiler seems one of them.

You can use malloc() from stdlib.h to allocate arrays dynamically.

int n;
printf("How many?");
scanf("%d", &n);

int* array = malloc(sizeof(*array) * n);
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

Variable length arrays require either:

  1. C99

  2. C11 and later with _STDC_NO_VLA__ not defined.


Array sizes must be more than 0.

int n;
printf("How many?");
if (scanf("%d", &n) == 1 && n > 0) {
  // Success
  int array[n];
  ...
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256