How to initialize all members of an array to the same value?

后端 未结 23 1938
清歌不尽
清歌不尽 2020-11-21 04:34

I have a large array in C (not C++ if that makes a difference). I want to initialize all members of the same value.

I could swear I

23条回答
  •  庸人自扰
    2020-11-21 05:06

    1. If your array is declared as static or is global, all the elements in the array already have default default value 0.
    2. Some compilers set array's the default to 0 in debug mode.
    3. It is easy to set default to 0 : int array[10] = {0};
    4. However, for other values, you have use memset() or loop;

    example: int array[10]; memset(array,-1, 10 *sizeof(int));

提交回复
热议问题