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

前端 未结 10 1267
甜味超标
甜味超标 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条回答
  •  Happy的楠姐
    2020-11-22 07:48

    You cannot do it. C compiler cannot do such a complex thing on stack.

    You have to use heap and dynamic allocation.

    What you really need to do:

    • compute size (nmsizeof(element)) of the memory you need
    • call malloc(size) to allocate the memory
    • create an accessor: int* access(ptr,x,y,rowSize) { return ptr + y*rowSize + x; }

    Use *access(boardAux, x, y, size) = 42 to interact with the matrix.

提交回复
热议问题