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

后端 未结 23 2808
清歌不尽
清歌不尽 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:21

    int i;
    for (i = 0; i < ARRAY_SIZE; ++i)
    {
      myArray[i] = VALUE;
    }
    

    I think this is better than

    int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5...
    

    incase the size of the array changes.

提交回复
热议问题