免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1831 | 回复: 5
打印 上一主题 下一主题

源代码疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-20 21:34 |只看该作者 |倒序浏览
#define FETCH_TYPE_PREFETCH 1
#define FETCH_TYPE_DEMAND   2
#define FETCH_TYPE_WRITE    4

typedef enum {
    ft_prefetch     = FETCH_TYPE_PREFETCH,
    ft_demand_read  = FETCH_TYPE_DEMAND,
    ft_demand_write = FETCH_TYPE_DEMAND | FETCH_TYPE_WRITE,
} fetch_ty

static char *fetch_type_names[] = {
    [ft_prefetch]     "prefetch",
    [ft_demand_read]  "demand read",
    [ft_demand_write] "demand write",
};
请问字符串前面加的方括号和常数是什么概念啊
谢谢

论坛徽章:
0
2 [报告]
发表于 2011-04-20 22:40 |只看该作者
比如 int a[] = {[2] 5, [4] 6,}; 初始化a[2] = 5;a[4] = 6;至于是在c99 还是gcc参考手册还是其他的书上介绍不记得了

论坛徽章:
0
3 [报告]
发表于 2011-04-20 22:50 |只看该作者
本帖最后由 djking1986 于 2011-04-20 22:52 编辑

gcc参考手册

6.25 Designated Initializers
Standard C90 requires the elements of an initializer to appear in a fixed order, the same as
the order of the elements in the array or structure being initialized.
   In ISO C99 you can give the elements in any order, specifying the array indices or structure
field names they apply to, and GNU C allows this as an extension in C90 mode as well.
This extension is not implemented in GNU C++.
   To specify an array index, write ‘[index] =’ before the element value. For example,
        int a[6] = { [4] = 29, [2] = 15 };
is equivalent to
        int a[6] = { 0, 0, 15, 0, 29, 0 };
The index values must be constant expressions, even if the array being initialized is automatic.
    An alternative syntax for this which has been obsolete since GCC 2.5 but GCC still
accepts is to write ‘[index]’ before the element value, with no ‘=’.
   To initialize a range of elements to the same value, write ‘[first ... last] = value’.
This is a GNU extension. For example,
       int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
If the value in it has side-effects, the side-effects will happen only once, not for each initialized
field by the range initializer.
Note that the length of the array is the highest value specified plus one.
   In a structure initializer, specify the name of a field to initialize with ‘.fieldname =’
before the element value. For example, given the following structure,
struct point { int x, y; };
the following initialization
struct point p = { .y = yvalue, .x = xvalue };
is equivalent to
struct point p = { xvalue, yvalue };
Another syntax which has the same meaning, obsolete since GCC 2.5, is ‘fieldname:’,
as shown here:
struct point p = { y: yvalue, x: xvalue };
The ‘[index]’ or ‘.fieldname’ is known as a designator. You can also use a designator
(or the obsolete colon syntax) when initializing a union, to specify which element of the
union should be used. For example,
union foo { int i; double d; };
union foo f = { .d = 4 };
will convert 4 to a double to store it in the union using the second element. By contrast,
casting 4 to type union foo would store it into the union as the integer i, since it is an
integer. (See Section 6.27 [Cast to Union], page 296.)
You can combine this technique of naming elements with ordinary C initialization of
successive elements. Each initializer element that does not have a designator applies to the
next consecutive element of the array or structure. For example,
int a[6] = { [1] = v1, v2, [4] = v4 };
is equivalent to
int a[6] = { 0, v1, v2, 0, v4, 0 };
Labeling the elements of an array initializer is especially useful when the indices are
characters or belong to an enum type. For example:
int whitespace[256]
= { [’ ’] = 1, [’\t’] = 1, [’\h’] = 1,
[’\f’] = 1, [’\n’] = 1, [’\r’] = 1 };
You can also write a series of ‘.fieldname’ and ‘[index]’ designators before an ‘=’ to
specify a nested subobject to initialize; the list is taken relative to the subobject corresponding
to the closest surrounding brace pair. For example, with the ‘struct point’ declaration
above:
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
If the same field is initialized multiple times, it will have value from the last initialization.
If any such overridden initialization has side-effect, it is unspecified whether the side-effect
happens or not. Currently, GCC will discard them and issue a warning.

评分

参与人数 1可用积分 +6 收起 理由
Godbach + 6 感谢分享

查看全部评分

论坛徽章:
0
4 [报告]
发表于 2011-04-20 23:47 |只看该作者
赞LS

论坛徽章:
36
IT运维版块每日发帖之星
日期:2016-04-10 06:20:00IT运维版块每日发帖之星
日期:2016-04-16 06:20:0015-16赛季CBA联赛之广东
日期:2016-04-16 19:59:32IT运维版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-19 06:20:00每日论坛发贴之星
日期:2016-04-19 06:20:00IT运维版块每日发帖之星
日期:2016-04-25 06:20:00IT运维版块每日发帖之星
日期:2016-05-06 06:20:00IT运维版块每日发帖之星
日期:2016-05-08 06:20:00IT运维版块每日发帖之星
日期:2016-05-13 06:20:00IT运维版块每日发帖之星
日期:2016-05-28 06:20:00每日论坛发贴之星
日期:2016-05-28 06:20:00
5 [报告]
发表于 2011-04-21 09:57 |只看该作者
C 的基础

论坛徽章:
0
6 [报告]
发表于 2011-04-21 11:11 |只看该作者
谢谢哈。。
平时写没用到过。。一个劲的以为是跟printk里面的参数一样有什么意义了,因为一直只看了C程序语言设计第二版。。GCC加了好多东西啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP