Why can't I create an array with size determined by a global variable?

前端 未结 7 717
北海茫月
北海茫月 2020-11-27 06:11

Why does the array a not get initialized by global variable size?

#include

int size = 5;

int main()
{
    int a[si         


        
7条回答
  •  醉酒成梦
    2020-11-27 06:30

    #include 
    
    /* int size=5; */
    #define size 5 /* use this instead*/
    /*OR*/
    int a[size]={1,2,3,4,5};  /* this*/
    
    int main() 
    { 
        int a[size]={1,2,3,4,5}; 
        printf("%d",a[0]); 
        return 0; 
    } 
    

    int size means that size is a variable and C does not allow variablesize arrays.

    I am using VS2008 where using

    const int size=5;  
    

    allows

    int a[size]={1,2,3,4,5}; 
    

提交回复
热议问题