如何初始化一个数组的所有成员为相同的值？

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

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

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

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

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

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

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

编辑：可能有用的扩展

（ Jonathan Leffler提供 ）

` `#define VAL_1(X) X #define VAL_2(X) VAL_1(X), VAL_1(X) /* etc. */` `

` `#define STRUCTVAL_1(...) { __VA_ARGS__ } #define STRUCTVAL_2(...) STRUCTVAL_1(__VA_ARGS__), STRUCTVAL_1(__VA_ARGS__) /*etc */` `

` `#define STRUCTVAL_48(...) STRUCTVAL_32(__VA_ARGS__), STRUCTVAL_16(__VA_ARGS__) struct Pair { char key[16]; char val[32]; }; struct Pair p_data[] = { STRUCTVAL_48("Key", "Value") }; int a_data[][4] = { STRUCTVAL_48(12, 19, 23, 37) };` `

macros名称是可协商的。

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

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

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

` `char* array[] = { [0] = "Hello", [1] = "World" };` `

` `enum { ERR_OK, ERR_FAIL, ERR_MEMORY }; #define _ITEM(x) [x] = #x char* array[] = { _ITEM(ERR_OK), _ITEM(ERR_FAIL), _ITEM(ERR_MEMORY) };` `

` `int i; for (i = 0; i < ARRAY_SIZE; ++i) { myArray[i] = VALUE; }` `

` `int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5...` `

memset为您提供了一个运行时命中的工作，但没有正确完成代码大小不受数组大小更改的影响。 在几乎所有情况下，当数组大于比如说几十个元素时，我都会使用这个解决scheme。

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

C-扩展

` `// put values 1-8, then two zeroes int list[10] = {1,2,3,4,5,6,7,8};` `

` `array = initial_value` `

• 初始化数组的第一个元素（通常的方式）
• 复制已经被设定的部分，没有被设定的部分，与下一个复制操作一起倍增大小

`loop runtime 0.004248 [seconds]`

`memfill() runtime 0.001085 [seconds]`

` `#include <stdio.h> #include <time.h> #include <string.h> #define ARR_SIZE 1000000 void memfill(void *dest, size_t destsize, size_t elemsize) { char *nextdest = (char *) dest + elemsize; size_t movesize, donesize = elemsize; destsize -= elemsize; while (destsize) { movesize = (donesize < destsize) ? donesize : destsize; memcpy(nextdest, dest, movesize); nextdest += movesize; destsize -= movesize; donesize += movesize; } } int main() { clock_t timeStart; double runTime; int i, a[ARR_SIZE]; timeStart = clock(); for (i = 0; i < ARR_SIZE; i++) a[i] = 9; runTime = (double)(clock() - timeStart) / (double)CLOCKS_PER_SEC; printf("loop runtime %f [seconds]\n",runTime); timeStart = clock(); a[0] = 10; memfill(a, sizeof(a), sizeof(a[0])); runTime = (double)(clock() - timeStart) / (double)CLOCKS_PER_SEC; printf("memfill() runtime %f [seconds]\n",runTime); return 0; }` `

` `void *memset(void *array, int value, unsigned sizeofarray);` `

` `#include <iostream> void PrintArray(int a[3][3]) { std::cout << "a11 = " << a[0][0] << "\t\t" << "a12 = " << a[0][1] << "\t\t" << "a13 = " << a[0][2] << std::endl; std::cout << "a21 = " << a[1][0] << "\t\t" << "a22 = " << a[1][1] << "\t\t" << "a23 = " << a[1][2] << std::endl; std::cout << "a31 = " << a[2][0] << "\t\t" << "a32 = " << a[2][1] << "\t\t" << "a33 = " << a[2][2] << std::endl; std::cout << std::endl; } int wmain(int argc, wchar_t * argv[]) { int a1[3][3] = { 11, 12, 13, // The most 21, 22, 23, // basic 31, 32, 33 }; // format. int a2[][3] = { 11, 12, 13, // The first (outer) dimension 21, 22, 23, // may be omitted. The compiler 31, 32, 33 }; // will automatically deduce it. int a3[3][3] = { {11, 12, 13}, // The elements of each {21, 22, 23}, // second (inner) dimension {31, 32, 33} }; // can be grouped together. int a4[][3] = { {11, 12, 13}, // Again, the first dimension {21, 22, 23}, // can be omitted when the {31, 32, 33} }; // inner elements are grouped. PrintArray(a1); PrintArray(a2); PrintArray(a3); PrintArray(a4); // This part shows in which order the elements are stored in the memory. int * b = (int *) a1; // The output is the same for the all four arrays. for (int i=0; i<9; i++) { std::cout << b[i] << '\t'; } return 0; }` `

` `a11 = 11 a12 = 12 a13 = 13 a21 = 21 a22 = 22 a23 = 23 a31 = 31 a32 = 32 a33 = 33 a11 = 11 a12 = 12 a13 = 13 a21 = 21 a22 = 22 a23 = 23 a31 = 31 a32 = 32 a33 = 33 a11 = 11 a12 = 12 a13 = 13 a21 = 21 a22 = 22 a23 = 23 a31 = 31 a32 = 32 a33 = 33 a11 = 11 a12 = 12 a13 = 13 a21 = 21 a22 = 22 a23 = 23 a31 = 31 a32 = 32 a33 = 33 11 12 13 21 22 23 31 32 33` `
1. 如果数组声明为静态或全局的，则数组中的所有元素都具有默认的默认值0。
2. 一些编译器在debugging模式下将数组的默认值设置为0。
3. 将default设置为0很容易：int array [10] = {0};
4. 但是，对于其他值，您可以使用memset（）或循环;

` `int i,value=5,array[1000]; for(i=0;i<1000;i++) array[i]=value;` `

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

` `#include <string.h> void array_init( void *start, size_t element_size, size_t elements, void *initval ){ memcpy( start, initval, element_size ); memcpy( (char*)start+element_size, start, element_size*(elements-1) ); } // testing #include <stdio.h> struct s { int a; char b; } array[2][3], init; int main(){ init = (struct s){.a = 3, .b = 'x'}; array_init( array, sizeof(array[0][0]), 2*3, &init ); for( int i=0; i<2; i++ ) for( int j=0; j<3; j++ ) printf("array[%i][%i].a = %i .b = '%c'\n",i,j,array[i][j].a,array[i][j].b); }` `

` `array[0][0].a = 3 .b = 'x' array[0][1].a = 3 .b = 'x' array[0][2].a = 3 .b = 'x' array[1][0].a = 3 .b = 'x' array[1][1].a = 3 .b = 'x' array[1][2].a = 3 .b = 'x'` `