博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to initialize an array in C
阅读量:4285 次
发布时间:2019-05-27

本文共 1239 字,大约阅读时间需要 4 分钟。

If your compiler is GCC you can use following syntax:

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

Check out detailed description: 

 

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

 

轉載自 

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

转载地址:http://mxsgi.baihongyu.com/

你可能感兴趣的文章
echarts(国产)基于html5-canvas的开源图表绘制组件
查看>>
Chart.Js轻量级HTML5图表插件
查看>>
基于Bootstrap的jQuery slider插件的使用bootstrap-slider.js
查看>>
Vue.js数据驱动的组件,为现代化的 Web 界面而生
查看>>
Bootstrap中文网开源项目免费 CDN 服务、cdn.bootcss.com
查看>>
C#发送Get请求(带参数)
查看>>
爬取Ip地址对应的物理位置等信息-百度服务器
查看>>
C# 获取IP地址
查看>>
C#使用ping命令
查看>>
C#域名操作,正则匹配域名
查看>>
VS调试版本和发布版本
查看>>
C#获取Url中的域名
查看>>
C# IP地址和整数之间的转换,IP地址和数字ip地址的转换
查看>>
packagecontrol.io 拒绝了我们的连接请求
查看>>
VSCode前端编辑器 1.7(编辑功能媲美sublime text,HTML等代码格式化很是不错)
查看>>
VsCode插件整理
查看>>
VsCode插件之vscode-icons
查看>>
Framework7特色的HTML框架WebApp开源前端框架
查看>>
VSCode插件之View In Browser/Open in Browser‘在浏览器中查看’
查看>>
Web前端代码编辑器之Atom整理
查看>>