1

I was trying to do this exercise code in initialization of arrays and I keep getting the error that's stated in the title. Researching online I've seen that defining SIZE as constant should do the job but it's not working for me. The thing is I've copied this code from my professor and it ran fine for him. Can you help me?

#include <stdio.h>

int main(void) {
    const int SIZE = 5; 
    
    int grades[SIZE] = {78, 67, 92, 83, 88};
    double sum;
    int i;
    
[...]

    }
S.B
  • 13,077
  • 10
  • 22
  • 49
Borato_al
  • 11
  • 1
  • 3

1 Answers1

1

Variable sized arrays cannot be initialized in the C language.

You need to initialize them manually

int main(void) {
    int SIZE = 5; 
    int grades[SIZE];
   
    memcpy(grades, (int[]){78, 67, 92, 83, 88}, sizeof(grades));

Your incompetent professor was actually using C++ compiler - but it is a different language and C++ compiler must not be used to compile C code

0___________
  • 60,014
  • 4
  • 34
  • 74