C在头文件中转发struct

Pov*_*asG 3 c struct forward-declaration

我试图struct在函数中传递指针.我有一个typedef在file1.h,并希望只将该头包含到file2.c,因为file2.h只需要指针.在C++中我会像我在这里写的一样,但是使用C99它不起作用.如果有人有任何建议如何传递struct没有完整定义的指针,将非常感激.编译器 - gcc.

file1.h

typedef struct
{
    ...
} NEW_STRUCT;
Run Code Online (Sandbox Code Playgroud)

file2.h

struct NEW_STRUCT;

void foo(NEW_STRUCT *new_struct); //error: unknown type name 'NEW_STRUCT'
Run Code Online (Sandbox Code Playgroud)

file2.c中

#include "file2.h"

#include "file1.h"

void foo(NEW_STRUCT *new_struct)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

小智 9

我想你只需要命名你的结构,并做一个前向声明,然后重新输入它.

第一档:

 typedef struct structName {} t_structName;
Run Code Online (Sandbox Code Playgroud)

第二档:

  struct stuctName;
  typedef struct structName t_structName
Run Code Online (Sandbox Code Playgroud)