问题描述
我有两个函数指针类型定义和两个结构体,struct pipe_s
和 struct 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 */
};
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)
效果很好。
如果我将 in
和 out
移入 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);
}