How to initialize an array to something in C without a loop?

后端 未结 7 1680
灰色年华
灰色年华 2021-01-02 06:58

Lets say I have an array like

int arr[10][10];

Now i want to initialize all elements of this array to 0. How can I do this without loops or

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 07:31

    The quick-n-dirty solution:

    int arr[10][10] = { 0 };
    

    If you initialise any element of the array, C will default-initialise any element that you don't explicitly specify. So the above code initialises the first element to zero, and C sets all the other elements to zero.

提交回复
热议问题