C compile error: “Variable-sized object may not be initialized”

前端 未结 10 1264
甜味超标
甜味超标 2020-11-22 07:22

Why do I receive the error \"Variable-sized object may not be initialized\" with the following code?

int boardAux[length][length] = {{0}};
10条回答
  •  故里飘歌
    2020-11-22 07:50

    This gives error:

    int len;
    scanf("%d",&len);
    char str[len]="";
    

    This also gives error:

    int len=5;
    char str[len]="";
    

    But this works fine:

    int len=5;
    char str[len]; //so the problem lies with assignment not declaration
    

    You need to put value in the following way:

    str[0]='a';
    str[1]='b'; //like that; and not like str="ab";
    

提交回复
热议问题