C在头文件中转发struct

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

file1.h

 typedef struct { ... } NEW_STRUCT; 

file2.h

 struct NEW_STRUCT; void foo(NEW_STRUCT *new_struct); //error: unknown type name 'NEW_STRUCT' 

file2.c中

 #include "file2.h" #include "file1.h" void foo(NEW_STRUCT *new_struct) { ... } 

我想你只需要命名你的结构,然后做一个前向声明,然后再重新定义它。

第一档:

  typedef struct structName {} t_structName; 

第二档:

  struct stuctName; typedef struct structName t_structName 

你可以试试这个:

file1.h

 typedef struct _NEW_STRUCT // changed! { ... } NEW_STRUCT; 

file2.h

 struct _NEW_STRUCT; // changed! void foo(struct _NEW_STRUCT *new_struct); // changed! 

file2.c中

 #include "file2.h" #include "file1.h" void foo(NEW_STRUCT *new_struct) { ... }