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

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

    Unless that value is 0 (in which case you can omit some part of the initializer and the corresponding elements will be initialized to 0), there's no easy way.

    Don't overlook the obvious solution, though:

    int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
    

    Elements with missing values will be initialized to 0:

    int myArray[10] = { 1, 2 }; // initialize to 1,2,0,0,0...
    

    So this will initialize all elements to 0:

    int myArray[10] = { 0 }; // all elements 0
    

    In C++, an empty initialization list will also initialize every element to 0. This is not allowed with C:

    int myArray[10] = {}; // all elements 0 in C++
    

    Remember that objects with static storage duration will initialize to 0 if no initializer is specified:

    static int myArray[10]; // all elements 0
    

    And that "0" doesn't necessarily mean "all-bits-zero", so using the above is better and more portable than memset(). (Floating point values will be initialized to +0, pointers to null value, etc.)

    0 讨论(0)
  • 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 <boost/preprocessor/repetition/enum.hpp>
    #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)
    
    0 讨论(0)
  • 2020-11-21 05:05

    For initializing 'normal' data types (like int arrays), you can use the bracket notation, but it will zero the values after the last if there is still space in the array:

    // put values 1-8, then two zeroes
    int list[10] = {1,2,3,4,5,6,7,8};
    
    0 讨论(0)
  • 2020-11-21 05:05
    #include<stdio.h>
    int main(){
    int i,a[50];
    for (i=0;i<50;i++){
        a[i]=5;// set value 5 to all the array index
    }
    for (i=0;i<50;i++)
    printf("%d\n",a[i]);
       return 0;
    }
    

    It will give the o/p 5 5 5 5 5 5 ...... till the size of whole array

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

    Back in the day (and I'm not saying it's a good idea), we'd set the first element and then:

    memcpy (&element [1], &element [0], sizeof (element)-sizeof (element [0]);

    Not even sure it would work any more (that would depend on the implementation of memcpy) but it works by repeatedly copying the initial element to the next - even works for arrays of structures.

    0 讨论(0)
  • 2020-11-21 05:06
    1. If your array is declared as static or is global, all the elements in the array already have default default value 0.
    2. Some compilers set array's the default to 0 in debug mode.
    3. It is easy to set default to 0 : int array[10] = {0};
    4. However, for other values, you have use memset() or loop;

    example: int array[10]; memset(array,-1, 10 *sizeof(int));

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