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

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

    You can do the whole static initializer thing as detailed above, but it can be a real bummer when your array size changes (when your array embiggens, if you don't add the appropriate extra initializers you get garbage).

    memset gives you a runtime hit for doing the work, but no code size hit done right is immune to array size changes. I would use this solution in nearly all cases when the array was larger than, say, a few dozen elements.

    If it was really important that the array was statically declared, I'd write a program to write the program for me and make it part of the build process.

提交回复
热议问题