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

前端 未结 7 718
北海茫月
北海茫月 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:27

    size is a variable, and C does not allow you to declare (edit: C99 allows you to declare them, just not initialize them like you are doing) arrays with variable size like that. If you want to create an array whose size is a variable, use malloc or make the size a constant.

提交回复
热议问题