在 C 中预定义结构

Predefine a struct in C

我尝试在 C 中这样做:

typedef struct s_match_fptr
{
  char *str;
  int  (*funcptr)(t_client *client, char **command);
} t_match_fptr;

typedef struct s_client
{
  int socket_fd;
  int port;
  char *server_ip;
  struct sockaddr_in s_in;
  t_match_fptr  *db;
} t_client;

重点是我尝试在我的 t_match_ptr 结构中声明一个函数指针,该函数指针接受一个 t_client 结构参数。

此外,我的结构 t_client 有一个数组 t_match_ptr

为简化起见,A 需要在 B 之后声明 AND B 需要在 A 之后声明。

那么,有没有办法在t_match_ptr声明之前"predeclare"t_client

谢谢你,抱歉英语不好。

转发声明。 在开头添加:typedef struct s_client t_client;

现在编译器在遇到 s_match_fptr 时会知道类型 t_client。 请注意,该类型只能在 s_match_fptr 定义中通过引用使用(即使用指针)。这样编译器在解析代码时不需要知道类型的实际内容。