Error C2057: expected constant expression

后端 未结 2 656
广开言路
广开言路 2021-01-17 18:05
if(stat(\"seek.pc.db\", &files) ==0 )
     sizes=files.st_size;

sizes=sizes/sizeof(int);
int s[sizes];

I am compiling this in Visual Studio 20

相关标签:
2条回答
  • 2021-01-17 18:26

    Size of an array must be a compile time constant. However, C99 supports variable length arrays. So instead for your code to work on your environment, if the size of the array is known at run-time then -

    int *s = malloc(sizes);
    // ....
    free s;
    

    Regarding the error message:

    int a[5];
       // ^ 5 is a constant expression
    
    int b = 10;
    int aa[b];
        // ^   b is a variable. So, it's value can differ at some other point.
    
    const int size = 5;
    int aaa[size];  // size is constant.
    
    0 讨论(0)
  • 2021-01-17 18:33

    The sizes of array variables in C must be known at compile time. If you know it only at run time you will have to malloc some memory yourself instead.

    0 讨论(0)
提交回复
热议问题