简体   繁体   English

包含函数指针的struct,其自身作为C中的返回类型

[英]struct containing a function pointer with itself as a return type in C

Got the following data struct: 得到以下数据结构:

typedef struct
{
    lamp *lamp;
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
    void (*func)(struct event *);
} event;

The last line inside the struct is supposed to be a pointer to a function with return type void with pointer to an event as an argument such as: 结构中的最后一行应该是一个指向函数的指针,返回类型为void,指向事件的指针作为参数,例如:

void function(event *evt);

Though, I get the following warning message: "its scope is only this definition or declaration, which is probably not what you want". 虽然,我得到以下警告信息:“它的范围只是这个定义或声明,可能不是你想要的”。 Is this right or wrong? 这是对还是错?

Your struct needs needs to be defined like this: 您的结构需要需要像这样定义:

typedef struct event  // <<< note the `event` tag here
{
    lamp *lamp;
    unsigned char a;
    unsigned char b;
    unsigned char c;
    unsigned char d;
    unsigned char e;
    void (*func)(struct event *);
} event;              // <<< you can still keep `event` as a typedef
                      //     which is equivalent to `struct event`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM