0
int main(void)
{
  const int SIZE= 5;
  int grades[SIZE]={78, 67, 92, 83, 88}; // variable-sized object may not be initialized
...
}

After compiling it, the computer shows " variable-sized object may not be initialized" and I dont want to replace SIZE with number 5 in the array, so can someone help me with this problem? Thanks in advance

dbush
  • 205,898
  • 23
  • 218
  • 273
  • That is because you are using a variable to determine the size. A variable is not a constant, hence is variable. – Paul Ogilvie Aug 27 '20 at 16:05
  • Instead of `const int` use `#define SIZE 5` – Paul Ogilvie Aug 27 '20 at 16:05
  • In C, a `const`-qualified variable is not a *constant expression*, so this is a *variable-length array* declaration, meaning its size is not determined until runtime. Among other limitations, VLAs may not be declared with an initializer. For this case, define `SIZE` as a macro as shown by Paul Ogilvie. – John Bode Aug 27 '20 at 16:09
  • In short, const variables in C can actually change in some cases, so the standard just says that what you are doing is wrong, even if it's correct in your particular case. – ElderBug Aug 27 '20 at 16:15

0 Answers0