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

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

    If the size of the array is known in advance, one could use a Boost preprocessor C_ARRAY_INITIALIZE macro to do the dirty job for you:

    #include 
    #define C_ARRAY_ELEMENT(z, index, name) name[index]
    #define C_ARRAY_EXPAND(name,size) BOOST_PP_ENUM(size,C_ARRAY_ELEMENT,name)
    #define C_ARRAY_VALUE(z, index, value) value
    #define C_ARRAY_INITIALIZE(value,size) BOOST_PP_ENUM(size,C_ARRAY_VALUE,value)
    

提交回复
热议问题