gpt4 book ai didi

C 双括号

转载 作者:行者123 更新时间:2023-12-05 09:01:11 26 4
gpt4 key购买 nike

我不知道这个“功能”叫什么,所以我无法用谷歌搜索它,如果标题没有意义,我也很抱歉。我最近查看了 suckless dwm 的源代码并看到了这段代码:(来自 dwm.c)

static int (*xerrorxlib)(Display *, XErrorEvent *);

还有这个:

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

void (*handler[LASTEvent]) (XEvent *) 是什么意思?它叫什么以及为什么使用它?

最佳答案

这个声明

static int (*xerrorxlib)(Display *, XErrorEvent *);

是指向函数指针的声明,名称为 xerrorxlib,(函数)具有返回类型 int 和两个指针类型的参数 Display * XErrorEvent *

指针具有静态存储持续时间。

这个声明

static void (*handler[LASTEvent]) (XEvent *) = {

声明一个名为 handler 的数组,该数组包含指向函数的指针的 LASTEvent 元素,返回类型为 void,参数类型为 XEvent *。该数组还具有静态存储持续时间。

至于这样的记录

[ButtonPress] = buttonpress,

然后在方括号中有一个被初始化的数组元素的索引。

例如你可以这样写

enum { First = 0, Second = 1 };

然后在像这样的数组声明中使用大括号初始化列表中的枚举数

int a[] =
{
[First] = 10,
[Second] = 20
};

在这种情况下,数组将有两个元素,其中索引为 0 的元素初始化为值 10,索引为 1 的元素初始化为值 20。

关于C 双括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73939616/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com