问题描述
在研究Wayland协议时,我发现了以结构类型作为参数的代码.
While studying wayland protocol, I found code that functions takes struct type as parameter.
#include <wayland-server.h>
static struct wl_compositor_interface compositor_interface =
{&compositor_create_surface, &compositor_create_region};
int main() {
wl_global_create (display, &wl_compositor_interface, 3, NULL,
&compositor_bind);
}
wl_global_create的签名是
signature of wl_global_create is
struct wl_global* wl_global_create (struct wl_display *display,
const struct wl_interface *interface,
int version,
void *data,
wl_global_bind_func_t bind)
wl_compositor_interface是结构类型,而不是变量名.但是wl_global_create()将结构类型作为函数参数.有人可以解释它的工作原理吗?
wl_compositor_interface is structure type, not a variable name. but wl_global_create() take structure type as function parameter.can someone explain how this works?
我阅读的源代码在这里. https://github.com/eyelash/tutorials/blob/master/wayland-compositor/wayland-compositor.c
the source code I read is here. https://github.com/eyelash/tutorials/blob/master/wayland-compositor/wayland-compositor.c
推荐答案
我浏览了源代码,同时有一个struct wl_compositor_interface
和一个变量wl_compositor_interface
.
I browsed through the source code, and there is both a struct wl_compositor_interface
and a variable wl_compositor_interface
.
包含的 wayland_server.h
包括,在底部的wayland-server-protocol.h
.不幸的是,它不能在线使用,而是在构建时生成的.您可以通过以下方式获得它:
The included wayland_server.h
includes, at the bottom, wayland-server-protocol.h
. Unfortunately, this is not available online, but is generated at build time. You can get it with:
$ git clone git://anongit.freedesktop.org/wayland/wayland
$ cd wayland
$ mkdir prefix
$ ./autogen.sh --prefix=$(pwd)/prefix --disable-documentation
$ make protocol/wayland-server-protocol.h
在此文件中,它具有(有些令人困惑)的定义:
In this file, it has the (somewhat confusing) definitions:
extern const struct wl_interface wl_compositor_interface; // On line 195
...
struct wl_compositor_interface { // Starting on line 986
void (*create_surface)(struct wl_client *client,
struct wl_resource *resource,
uint32_t id);
void (*create_region)(struct wl_client *client,
struct wl_resource *resource,
uint32_t id);
};
第一次引用的是struct
,第二次引用的是变量.
It's the struct
that's being referenced the first time, and the variable the second.
这篇关于结构类型本身可以传递给函数作为C中的参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!