initialize a multidimensional C array of variable size to zero

后端 未结 5 1995
小蘑菇
小蘑菇 2021-02-08 07:03

I want to initialize a two-dimensional array of variable size to zero. I know it can be done for a fixed-sized array:

int myarray[10][10] = {0};
<
5条回答
  •  走了就别回头了
    2021-02-08 07:33

    You can't create a static array using non-constant variables. Try using dynamic allocation:

    int i = 10;
    int j = 10;
    size_t nbytes = i*j*sizeof(int);
    int* myarray = (int*) malloc(nbytes);
    memset(myarray,0,nbytes);
    

提交回复
热议问题