我正在尝试在函数中传递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)
{
...
}
答案 0 :(得分:9)
我认为你只需要命名你的结构,然后做一个前向声明,然后重新输入它。
第一档:
typedef struct structName {} t_structName;
第二档:
struct stuctName;
typedef struct structName t_structName
答案 1 :(得分:0)
你可以试试这个:
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)
{
...
}