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

前端 未结 10 1273
甜味超标
甜味超标 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:56

    I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length is not a compile time constant).

    You must manually initialize that array:

    int boardAux[length][length];
    memset( boardAux, 0, length*length*sizeof(int) );
    

提交回复
热议问题