1

我有两个用于函数指针和两个结构的 typedef,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结构并从那里调用它们,我会得到同样的错误,所以位置似乎并不重要。

有任何想法吗?

4

1 回答 1

2

在引用它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);
}
于 2021-02-16T11:04:46.780 回答