initialize a multidimensional C array of variable size to zero

后端 未结 5 1993
小蘑菇
小蘑菇 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 cannot initialize it with an initializer, but you can memset() the array to 0.

    #include 
    
    int main(void) {
      int a = 13, b = 42;
      int m[a][b];
      memset(m, 0, sizeof m);
      return 0;
    }
    

    Note: this is C99. In C89 the declaration of m ( int m[a][b]; ) is an error.

提交回复
热议问题