Zer*_*day 2 c function-pointers declaration ctor-initializer
我不知道这个“功能”叫什么,所以我也无法用谷歌搜索它,如果标题没有意义,我很抱歉。我最近查看了suckless dwm的源代码并看到了这段代码:(来自dwm.c)
static int (*xerrorxlib)(Display *, XErrorEvent *);
Run Code Online (Sandbox Code Playgroud)
还有这个:
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[KeyRelease] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify
};
Run Code Online (Sandbox Code Playgroud)
这是什么void (*handler[LASTEvent]) (XEvent *)意思 ?它叫什么以及为什么用它?
本声明
static int (*xerrorxlib)(Display *, XErrorEvent *);
Run Code Online (Sandbox Code Playgroud)
是一个指向函数的指针的声明,其名称xerrorxlib为(函数)具有返回类型int和两个指针类型的参数Display *和XErrorEvent *。
指针具有静态存储期限。
本声明
static void (*handler[LASTEvent]) (XEvent *) = {
Run Code Online (Sandbox Code Playgroud)
声明一个数组,其中包含指向函数的指针元素handler的名称,以及返回类型和参数类型。该数组还具有静态存储持续时间。LASTEventvoidXEvent *
至于这样的记录
[ButtonPress] = buttonpress,
Run Code Online (Sandbox Code Playgroud)
然后方括号中是已初始化的数组元素的索引。
例如你可以写
enum { First = 0, Second = 1 };
Run Code Online (Sandbox Code Playgroud)
然后在数组声明中使用大括号初始化列表中的枚举器,例如
int a[] =
{
[First] = 10,
[Second] = 20
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,数组将有两个元素,其中索引为 0 的元素由值 10 初始化,索引为 1 的元素由值 20 初始化。
| 归档时间: |
|
| 查看次数: |
159 次 |
| 最近记录: |