免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: vbs100
打印 上一主题 下一主题

请问下这是什么语法? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-08-27 16:41 |只看该作者

回复 #1 vbs100 的帖子

C89 能通过?
真的吗? 我怎么记得这是C99的新东西?

论坛徽章:
1
寅虎
日期:2014-11-30 21:25:54
12 [报告]
发表于 2009-08-27 16:47 |只看该作者
gcc --std=c89 是可以通过的 但这个功能好像真是c99才引进的

下面引自 c - in a nushell
8.3.2. Initializing Specific Elements
C99 has introduced element designators to allow you to associate initializers with specific elements . To specify a certain element to initialize, place its index in square brackets. In other words, the general form of an element designator for array elements is:
[constant_expression]

The index must be an integer constant expression. In the following example, the element designator is [A_SIZE/2]:
#define A_SIZE 20
int a[A_SIZE] = { 1, 2, [A_SIZE/2] = 1, 2 };

This array definition initializes the elements a[0] and a[10] with the value 1, and the elements a[1] and a[11] with the value 2. All other elements of the array will be given the initial value 0. As this example illustrates, initializers without an element designator are associated with the element following the last one initialized.
If you define an array without specifying its length, the index in an element designator can have any non-negative integer value. As a result, the following definition creates an array of 1,001 elements:
int a[ ] = { [1000] = -1 };

论坛徽章:
0
13 [报告]
发表于 2009-08-27 16:48 |只看该作者

回复 #12 vbs100 的帖子

为什么可以通过我不知道, 但是刚刚查了一下, 确实是C99的新功能`

论坛徽章:
0
14 [报告]
发表于 2009-08-27 16:52 |只看该作者

回复 #12 vbs100 的帖子

学习了, 还可以int a[] = {[1000] = -1};

论坛徽章:
0
15 [报告]
发表于 2009-08-27 17:36 |只看该作者
动手试试不就知道了嘛~~ 呵呵

int main()
{
    int a[] = {
       [0] = 10,
       [1] = 1,
       [9] = 0
    };

    int i;
    for(i=0; i < 10; i++)
       printf("%d -- %d\n",i,a);

   return 0;
}

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
16 [报告]
发表于 2009-08-27 18:00 |只看该作者

回复 #12 vbs100 的帖子

原帖由 vbs100 于 2009-8-27 16:47 发表
gcc --std=c89 是可以通过的 但这个功能好像真是c99才引进的


gcc -std=c89 -pedantic

btw:
不小心点了2楼koolcoy一个臭蛋……  sorry啊

论坛徽章:
1
寅虎
日期:2014-11-30 21:25:54
17 [报告]
发表于 2009-08-28 08:59 |只看该作者

回复 #16 OwnWaterloo 的帖子

原来如此啊 呵呵  多谢指点

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
18 [报告]
发表于 2009-08-28 10:14 |只看该作者

回复 #16 OwnWaterloo 的帖子

还了你一个

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
19 [报告]
发表于 2009-08-28 12:27 |只看该作者

回复 #17 vbs100 的帖子

应该谢谢楼上这位报复心很强同学~~~
我也是从他的回帖子中看到这个选项的。

to koolcoy:
早知道不告诉你

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
20 [报告]
发表于 2009-08-28 12:34 |只看该作者
原帖由 OwnWaterloo 于 2009-8-28 12:27 发表

to koolcoy:
早知道不告诉你

但是你已经告诉我了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP