源代码疑问

发布于 2022-10-15 09:00:53 字数 700 浏览 15 评论 0

#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",
};
请问字符串前面加的方括号和常数是什么概念啊
谢谢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦冥 2022-10-22 09:00:54

本帖最后由 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.

成熟的代价 2022-10-22 09:00:54

赞LS

难以启齿的温柔 2022-10-22 09:00:54

C 的基础

_畞蕅 2022-10-22 09:00:54

谢谢哈。。
平时写没用到过。。一个劲的以为是跟printk里面的参数一样有什么意义了,因为一直只看了C程序语言设计第二版。。GCC加了好多东西啊

烏雲後面有陽光 2022-10-22 09:00:53

比如 int a[] = {[2] 5, [4] 6,}; 初始化a[2] = 5;a[4] = 6;至于是在c99 还是gcc参考手册还是其他的书上介绍不记得了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文