![](/img/trans.png)
[英]Difference between “struct foo*” and “foo*” where foo is a struct?
[英]expected struct foo* but argument is of type struct foo* for function pointers
我有两个用于 function 指针的类型定义和两个结构, 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 */
};
在我的实现中,我有一个 function 尝试in
以下位置调用 function:
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)
效果很好。
如果我out
in
结构并从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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.