views:

60364

answers:

13

I have a large array in C (not C++ if that makes a difference). I want to initialize all members to the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, but isn't there a way to do this that is built right into the C syntax?

A: 

Isn't it

int myarr[100] = { value };
jim
myarr isn't an array in your case...
QuantumPete
It looks like an array now. The bigger problem is that the initializer only sets the first element of the array, not every value in the array.
Jonathan Leffler
Yup - getting my languages mixed up..
jim
looks like a valid answer, what's the reason for the down vote!
Narendra N
+45  A: 

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 };

Edit:

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

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.)

aib
Reading through the C++ standard, you can also do int array[10] = {}; to zero initialise. I don't have the C standard to check this is valid C as well though.
workmad3
Looking at section 6.7.8 Initialization of the C99 standard, it does not appear that an empty initializer list is allowed.
Jonathan Leffler
C99 has a lot of nice features for structure and array initialization; the one feature it does not have (but Fortran IV, 1966, had) is a way to repeat a particular initializer for an array.
Jonathan Leffler
+2  A: 

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};
warren
+4  A: 

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.

plinth
+2  A: 
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.

Tarski
+1  A: 

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.

ddimitrov
+2  A: 

If you want to ensure that every member of the array is explicitly initialized, just omit the dimension from the declaration:

int myArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

The compiler will deduce the dimension from the initializer list. Unfortunately, for multidimensional arrays only the outermost dimension may be omitted:

int myPoints[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} };

is OK, but

int myPoints[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} };

is not.

Frank Szczerba
+1  A: 

Here is another way:

static void
unhandled_interrupt(struct trap_frame *frame, int irq, void *arg)
{
    //this code intentionally left blank
}

static struct irqtbl_s vector_tbl[XCHAL_NUM_INTERRUPTS] = {
    [0 ... XCHAL_NUM_INTERRUPTS-1] {unhandled_interrupt, NULL},
};

See:

C-Extensions

Designated inits

Then ask the question: When can one use C extensions?

The code sample above is in an embedded system and will never see the light from another compiler.

humble_guru
+20  A: 

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

qrdl
A: 
sambowry
This is such an old question...
GMan
But new solution.
sambowry
I'm dubious of whether or not this is a solution. I'm not sure whether `sizeof(void)` is even valid.
Chris Lutz
It doesn't work. Only the first two are initialised, the remainder are all uninitialised. I'm using GCC 4.0 on Mac OS X 10.4.
dreamlax
+7  A: 

For statically initializing a large array with the same value, without multiple copy-paste, you can use macros:

#define VAL_1X     42
#define VAL_2X     VAL_1X,  VAL_1X
#define VAL_4X     VAL_2X,  VAL_2X
#define VAL_8X     VAL_4X,  VAL_4X
#define VAL_16X    VAL_8X,  VAL_8X
#define VAL_32X    VAL_16X, VAL_16X
#define VAL_64X    VAL_32X, VAL_32X

int myArray[53] = { VAL_32X, VAL_16X, VAL_4X, VAL_1X };

If you need to change the value, you have to do the replacement at only one place.

mouviciel
I would only consider this in extreme cases, surely a memset is the more elegant way to express it.
kaizer.se
Of course memset() is the way to go. I understood that OP was looking for an alternative.
mouviciel
If the data must be ROM-able, memset can not be used.
Amigable Clark Kant
Preprocessor will actually generate the code from #defines. With larger array dimensions the executable size will grow. But definitely + for the idea ;)
Leonid
A: 

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.

High Performance Mark
A: 

How do we use analyzeIntegerArray to write this c++ programming

How many integers? 3

Enter integer #1: 12795 Enter integer #2: -2784 Enter integer #3: -27904

The smallest digit: 0 Digit 1 can be found in integer number(s): 3

The largest digit: 9 Digit 9 can be found in integer number(s): 1, 3

man