Initialization of arrays in C

前端 未结 7 1048
难免孤独
难免孤独 2021-01-21 23:57

In C, I have read that half-initialized arrays will be filled with zeros for the rest of the elements (irrespective of integer or char arrays).

E.g.:

in         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 00:13

    If you want to be sure that your code will work with all compilers you should initialize all your array elements just like it:

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

    If the number of elements of your array is too high (100 or 10000) the best solution becomes to initialize it dynamicaly at the runtime.

提交回复
热议问题