20130507一维数组初始化

来源:互联网 发布:淘宝使劲互刷收藏怕吗 编辑:程序博客网 时间:2024/04/28 17:38

From stackoverflow.com

How to initialize an array in C:

Initialize all members to the same value:

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:

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

Objects with static storage duration will initialize to 0 if no initializer is specified:

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

If your compiler is GCC you can use following syntax:

int array[1024] = {[0 ... 1023] = 5};int A[10] = {[0 ... 4] = 5, [5 ... 9] = 3};
原创粉丝点击