How to initialize an array in C
来源:互联网 发布:阿里云dns没有百度稳定 编辑:程序博客网 时间:2024/05/18 03:56
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
#include <stdio.h>#include <stdlib.h>intmain(){ int i = 0; int array[100] = { [0 ... 99] = 5}; for (i = 0; i < 100; i++) { printf("array[%d] = %d\n", i, array[i]); } return 0;}
轉載自 http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c
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:
int myArray[10]={};//all elements 0 in C++
Remember that objects with static storage duration will initialize to 0 if no initializer is specified:
staticint 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.)
- How to initialize an array in C
- How to define an array in c#.
- How to format strings in an array in ruby?
- How to initialize a static const map in c++?
- How to call different methods as given in an array
- How to group anagrams in a string into an array
- How to sum values in an array with different hash
- How to initialize an STL vector
- How to split a string to array in objective-c?
- How to copy an object in objective c
- how to remove nil and blank string in an array in Ruby
- How to sort an array in Ruby to a particular order?
- How to remove Duplicates from an Array
- How to find number of a specific integer in an array of Fixnums ruby
- How to Check if an Array Contains a Value in Java Efficiently?
- How to sensibly split an array around an object
- How Would You Get the Count of an Array in C++?
- How Would You Get the Count of an Array in C++?
- 跟我一起写 Makefile(六) 转自http://blog.csdn.net/haoel/article/details/2891
- 十六周项目1
- JS对联浮动效果
- ./configure --prefix=/otp/
- ORM Optimize
- How to initialize an array in C
- 网游服务器应分离实时功能
- 针的运用
- 简单的方法制作32*32图标
- 关于cxf方式访问webservice的问题
- writel()和readl()函数的作用
- tomcat access log
- (转)逆向与反汇编工具
- 跟我一起写 Makefile(七) 转自http://blog.csdn.net/haoel/article/details/2892