微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

预期的 struct foo* 但参数是函数指针的 struct foo* 类型

如何解决预期的 struct foo* 但参数是函数指针的 struct foo* 类型

我有两个函数指针类型定义和两个结构体,struct pipe_sstruct pipe_buffer_s 定义如下:

typedef void (*pipe_inf_t)(struct pipe_buffer_s *);
typedef void (*pipe_outf_t)(struct pipe_buffer_s *);

struct
pipe_buffer_s
{
    size_t cnt;      /* number of chars in buffer */
    size_t len;      /* length of buffer */
    uint8_t *mem;    /* buffer */
};

struct
pipe_s
{
    struct pipe_buffer_s buf;
    uint8_t state;
    pipe_inf_t in;   /* input call */
    pipe_outf_t out; /* output call */
};

在我的实现中,我有一个函数试图调用函数 in

void
pipe_receive(struct pipe_s *pipe)
{
    pipe_inf_t in;
    in = pipe->in;
    in(&pipe->buf);
}

但我收到了奇怪的错误

pipe.c:107:5: 注意:预期为 'struct pipe_buffer_s *' 但参数的类型为 'struct pipe_buffer_s *'

这对我来说毫无意义。据我所知,告诉,我没有搞砸并尝试使用未定义长度的结构,因为我在这里只使用指针。我想我的 typedef 可能有问题...

将 typedef 更改为 typedef void (*pipe_inf_t)(int);调用 in(5) 效果很好。

如果我将 inout 移入 pipe_buffer_s 结构并从那里调用它们,因此位置似乎无关紧要,我会得到同样的错误

有什么想法吗?

解决方法

在引用它之前添加pipe_buffer_s的定义。这可能是不完整的类型:


#include <stdlib.h>
#include <stdint.h>

struct pipe_buffer_s; // Incomplete definition

typedef void (*pipe_inf_t)(struct pipe_buffer_s *);
typedef void (*pipe_outf_t)(struct pipe_buffer_s *);

struct
pipe_buffer_s
{
    size_t cnt;      /* number of chars in buffer */
    size_t len;      /* length of buffer */
    uint8_t *mem;    /* buffer */
};

struct
pipe_s
{
    struct pipe_buffer_s buf;
    uint8_t state;
    pipe_inf_t in;   /* input call */
    pipe_outf_t out; /* output call */
};

// In my implementation,I have a function that attempts to call the function in:

void
pipe_receive(struct pipe_s *pipe)
{
    pipe_inf_t in;
    in = pipe->in;
    in(&pipe->buf);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。