能结构类型本身传递给函数在C参数?(can struct type itself be passed

2019-09-28 16:13发布

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

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 is structure type, not a variable name. but wl_global_create() take structure type as function parameter. can someone explain how this works?

the source code I read is here. https://github.com/eyelash/tutorials/blob/master/wayland-compositor/wayland-compositor.c

Answer 1:

我通过源代码浏览,并且存在既是struct wl_compositor_interface和可变wl_compositor_interface

所包括的wayland_server.h包括,在底部, wayland-server-protocol.h 。 不幸的是,这是不是在网上,而是在构建时生成。 你可以得到它:

$ 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

在这个文件中,它具有(有点混乱)的定义:

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是真实所引用的第一时间,变量第二。



文章来源: can struct type itself be passed to function as parameter in c?
标签: c wayland