C语言 如何在 .h 中转发 typedef 结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7259238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:32:27  来源:igfitidea点击:

How to forward typedef'd struct in .h

ctypedefforward-declaration

提问by erandros

I have Preprocessor.h

我有 Preprocessor.h

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

I realized then that I had to separate declarations from definitions. So I created Preprocessor.c:

然后我意识到我必须将声明与定义分开。所以我创建了 Preprocessor.c:

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

typedef struct Preprocessor Prepro;

And Preprocessor.h is now:

Preprocessor.h 现在是:

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

That obviously, doesn't work because Pr..h doesn't know Prepro type. I already tried several combinations, none of them worked. I can't find the solution.

这显然不起作用,因为 Pr..h 不知道 Prepro 类型。我已经尝试了几种组合,但都没有奏效。我找不到解决方案。

回答by Joe

Move the typedef struct Preprocessor Prepro;to the header the file and the definition in the c file along with the Prepro_init definition. This is will forward declare it for you with no issues.

typedef struct Preprocessor Prepro;文件和 c 文件中的定义与 Prepro_init 定义一起移至标题。这将毫无问题地为您转发声明。

Preprocessor.h

预处理器.h

#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_

#define MAX_FILES 15

typedef struct Preprocessor Prepro;

void Prepro_init(Prepro* p);

#endif

Preprocessor.c

预处理器

#include "Preprocessor.h"

#include <stdio.h>

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};

void Prepro_init(Prepro* p) {
    (*p).currentFile = 0;
}

回答by Sander De Dycker

If you want to hide the definition of Preprocessor, you can simply put this in the header file :

如果你想隐藏 的定义Preprocessor,你可以简单地把它放在头文件中:

struct Preprocessor;
typedef struct Preprocessor Prepro;

But more generally, you'll probably also need the Preprocessordefinition in the header file, to allow other code to actually use it.

但更一般地,您可能还需要Preprocessor头文件中的定义,以允许其他代码实际使用它。

回答by hamstergene

You have put in .cwhat should be in .h, and vice versa. Prepro_initmust be in .cfile, and that file must #include "Preprocessor.h".

您已经放入.c了应该放入的内容.h,反之亦然。Prepro_init必须在.c文件中,并且该文件必须#include "Preprocessor.h".

回答by Muthu Ganapathy Nathan

YAS:Yet Another Solution.

YAS:又一个解决方案。

Preprocessor.h

预处理器.h

<some code>
    void Prepro_init(Prepro* p) {
        (*p).currentFile = 0;
    }
<some code>

Preprocessor.c

预处理器

#define MAX_FILES 15

struct Preprocessor {
    FILE fileVector[MAX_FILES];
    int currentFile;
};
typedef struct Preprocessor Prepro;

#include "Preprocessor.h"      //include after defining your structure.

         <some code> 
        {
              struct Prepro p;
              Prepro_init(p);
     <some code> 
          .... using p.currentFile.....
          .....using other members....
     <some code> 
        }
           <some code> 

Now it will work. I think this is your requirement. Hope it helps.

现在它将起作用。我想这是你的要求。希望能帮助到你。

Drawback:The members of the structure Preprocessor, must be predetermined. i.e the header file uses the member currentFile. So, c file which includes Preprocessor.h must have a structure which is typedefined as Prepro and that structure must include a member currentFile.(in this case).

缺点:结构 Preprocessor 的成员,必须预先确定。即头文件使用成员 currentFile。因此,包含 Preprocessor.h 的 c 文件必须具有类型定义为 Prepro 的结构,并且该结构必须包含成员 currentFile.(在本例中)。

The same problem I had a year before, while writting a header file to display a users Avl tree in a graphical tree format.

我一年前遇到了同样的问题,同时编写头文件以图形树格式显示用户 Avl 树。

回答by user877329

I would suggest that you follow Linus[1], and do not use typedef struct.

我建议您遵循 Linus[1],并且不要使用 typedef 结构。

If you are in control over the library:

如果您可以控制库:

Include file

包含文件

struct Foo;

int foo_get_n(const struct Foo *bar);

In implementation file

在实现文件中

struct Foo
{int n;};

int foo_get_n(const struct Foo *bar)
{return bar->n;}

If you are not in control over the library:

如果您无法控制库:

Ask the maintainer to remove these polluting typedefs from the include files.

要求维护者从包含文件中删除这些有污染的 typedef。

Summary

概括

Do not use typedef struct.

不要使用 typedef 结构。

[1] http://yarchive.net/comp/linux/typedefs.html

[1] http://yarchive.net/comp/linux/typedefs.html

回答by Karoly Horvath

Swap .hand .cfile. Include header in .c.

交换.h.c归档。在.c.

Also refer to a book about declarations, definitions and what header files do.

另请参阅有关声明、定义和头文件功能的书。