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?
views:
60364answers:
13Unless 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.)
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};
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.
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.
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.
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:
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.
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
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.
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.
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