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

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

    A slightly tongue-in-cheek answer; write the statement

    array = initial_value
    

    in your favourite array-capable language (mine is Fortran, but there are many others), and link it to your C code. You'd probably want to wrap it up to be an external function.

    0 讨论(0)
  • 2020-11-21 05:20

    I know the original question explicitly mentions C and not C++, but if you (like me) came here looking for a solution for C++ arrays, here's a neat trick:

    If your compiler supports fold expressions, you can use template magic and std::index_sequence to generate an initializer list with the value that you want. And you can even constexpr it and feel like a boss:

    #include <array>
    
    /// [3]
    /// This functions's only purpose is to ignore the index given as the second
    /// template argument and to always produce the value passed in.
    template<class T, size_t /*ignored*/>
    constexpr T identity_func(const T& value) {
        return value;
    }
    
    /// [2]
    /// At this point, we have a list of indices that we can unfold
    /// into an initializer list using the `identity_func` above.
    template<class T, size_t... Indices>
    constexpr std::array<T, sizeof...(Indices)>
    make_array_of_impl(const T& value, std::index_sequence<Indices...>) {
        return {identity_func<T, Indices>(value)...};
    }
    
    /// [1]
    /// This is the user-facing function.
    /// The template arguments are swapped compared to the order used
    /// for std::array, this way we can let the compiler infer the type
    /// from the given value but still define it explicitly if we want to.
    template<size_t Size, class T>
    constexpr std::array<T, Size> 
    make_array_of(const T& value) {
        using Indices = std::make_index_sequence<Size>;
        return make_array_of_impl(value, Indices{});
    }
    
    // std::array<int, 4>{42, 42, 42, 42}
    constexpr auto test_array = make_array_of<4/*, int*/>(42);
    static_assert(test_array[0] == 42);
    static_assert(test_array[1] == 42);
    static_assert(test_array[2] == 42);
    static_assert(test_array[3] == 42);
    // static_assert(test_array[4] == 42); out of bounds
    

    You can take a look at the code at work (at Wandbox)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:22

    If the array happens to be int or anything with the size of int or your mem-pattern's size fits exact times into an int (i.e. all zeroes or 0xA5A5A5A5), the best way is to use memset().

    Otherwise call memcpy() in a loop moving the index.

    0 讨论(0)
  • 2020-11-21 05:22

    If you mean in parallel, I think the comma operator when used in conjunction with an expression can do that:

    a[1]=1, a[2]=2, ..., a[indexSize]; 
    

    or if you mean in a single construct, you could do that in a for loop:

    for(int index = 0, value = 10; index < sizeof(array)/sizeof(array[0]); index++, value--)
      array[index] = index;
    

    //Note the comma operator in an arguments list is not the parallel operator described above;

    You can initialize an array decleration:

    array[] = {1, 2, 3, 4, 5};
    

    You can make a call to malloc/calloc/sbrk/alloca/etc to allocate a fixed region of storage to an object:

    int *array = malloc(sizeof(int)*numberOfListElements/Indexes);
    

    and access the members by:

    *(array + index)
    

    Etc.

    0 讨论(0)
  • 2020-11-21 05:23

    If your compiler is GCC you can use following syntax:

    int array[1024] = {[0 ... 1023] = 5};
    

    Check out detailed description: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits.html

    0 讨论(0)
提交回复
热议问题