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

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

    Cutting through all the chatter, the short answer is that if you turn on optimization at compile time you won't do better than this:

    int i,value=5,array[1000]; 
    for(i=0;i<1000;i++) array[i]=value; 
    

    Added bonus: the code is actually legible :)

提交回复
热议问题