在头结构的C正向声明 [英] C forward declaration of struct in header

查看:126
本文介绍了在头结构的C正向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过结构指针功能。我在file1.h有一个的typedef ,并希望只包括头file2.c中,因为只有file2.h需要指针。在C ++中我只写像我一样在这里,但使用C99这是行不通的。如果有人有任何建议如何通过结构指针没有完整的定义将是非常美联社preciated。编译器 - GCC。

file1.h

  typedef结构
{
    ...
} NEW_STRUCT;

file2.h

 结构NEW_STRUCT;无效美孚(NEW_STRUCT * new_struct); //错误:未知类型名称'NEW_STRUCT  

file2.c中

 的#includefile2.h#包括file1.h无效美孚(NEW_STRUCT * new_struct)
{
    ...
}


解决方案

我觉得你只需要命名的结构,并执行它,并重新的typedef后向前声明。

第一个文件:

  typedef结构structName {} t_structName;

第二个文件:

 结构stuctName;
  typedef结构structName t_structName

I am trying to pass struct pointer in function. I have a typedef in file1.h, and want to only include that header to file2.c, because file2.h only need pointer. In C++ I would just write like I did here, but using C99 it doesn't work. If someone has any suggestions how to pass struct pointer without full definition it would be very appreciated. Compiler - 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)
{
    ...
}

解决方案

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

这篇关于在头结构的C正向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆