can struct type itself be passed to function as parameter in c?

拟墨画扇 提交于 2019-12-11 04:17: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


回答1:


I browsed through the source code, and there is both a struct wl_compositor_interface and a variable wl_compositor_interface.

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

It's the struct that's being referenced the first time, and the variable the second.



来源:https://stackoverflow.com/questions/44101813/can-struct-type-itself-be-passed-to-function-as-parameter-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!