6

我正在尝试struct在函数中传递指针。我typedef在 file1.h 中有一个,并且只想将该标头包含到 file2.c,因为 file2.h 只需要指针。在 C++ 中,我会像在此处那样编写,但使用 C99 则行不通。如果有人对如何在struct没有完整定义的情况下传递指针有任何建议,将不胜感激。编译器 - gcc。

文件1.h

typedef struct
{
    ...
} NEW_STRUCT;

文件2.h

struct NEW_STRUCT;

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

文件2.c

#include "file2.h"

#include "file1.h"

void foo(NEW_STRUCT *new_struct)
{
    ...
}
4

2 回答 2

10

I think you just have to name your structure, and do a forward declaration of it and after re typedef it.

First file:

 typedef struct structName {} t_structName;

Second file:

  struct stuctName;
  typedef struct structName t_structName
于 2013-07-28T23:13:44.257 回答
1

你可以试试这个:

文件1.h

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

文件2.h

struct _NEW_STRUCT; // changed!

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

文件2.c

#include "file2.h"
#include "file1.h"

void foo(NEW_STRUCT *new_struct)
{
    ...
}
于 2015-01-08T14:32:11.953 回答