繁体   English   中英

C在头文件中转发struct

[英]C forward declaration of struct in header

我试图在函数中传递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)
{
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM