如何正确定义结构的函数指针,这需要结构作为一个指针? [英] How to properly define a function pointer in struct, which takes struct as a pointer?

查看:132
本文介绍了如何正确定义结构的函数指针,这需要结构作为一个指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回调函数的结构体,回调函数需要一个指向结构,以尽自己的操作。如何正确定义这些元素,那就是将编译没有警告?

I have a struct with a callback function, the callback function needs a pointer to the structure in order to do its operation. How do I properly define these elements such that is will compile without warnings?

typedef struct {

    // some fields required for processing...

    int (*doAction)(struct pr_PendingResponseItem *pr);
} pr_PendingResponseItem;

如果我删除的公关参数结构的属性,我得到一个错误。如果我留在,我得到一个警告:
它的作用域仅限于此定义或声明,这可能不是你想要的

If I remove the "struct" attribute on the pr parameter, I get an error. If I leave it in, I get a warning: "its scope is only this definition or declaration, which is probably not what you want"

这一切的作品,但我想知道正确的方式来定义这样的结构。

It all works, but I would like to know the proper way to define such a structure.

另外有关,正在制定一个自我参照结构:

Also related, is defining a self referential structure:

typedef struct LinkedItem_ {
    LinkedItem_ * prev;
    LinkedItem_ * next;
    void * data;
} LinkedItem;

(我认为这是正确的,但如果是涉及到的问题更多的想法是值得欢迎的。)

(I think this is correct, but additional thoughts are welcome if it is related to the question.)

推荐答案

您的函数指针引用结构pr_PendingResponseItem,但是你有没有宣布一个结构pr_PendingResponseItem。你只需要Typedef的这个名字pr_PendingResponseItem一位不愿透露姓名的结构(这个名字尚未确定)。

Your function pointer references a struct pr_PendingResponseItem, but you haven't declared a struct pr_PendingResponseItem. You just have an unnamed structure typedef'ed to the name pr_PendingResponseItem (and that name isn't established yet).

使该结构的名称:

struct pr_PendingResponseItem {

    // some fields required for processing...

    int (*doAction)(struct pr_PendingResponseItem *pr);
} ;
typedef struct pr_PendingResponseItem pr_PendingResponseItem;

这篇关于如何正确定义结构的函数指针,这需要结构作为一个指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆