C 如何转发typedef';d结构in.h
我有 预处理器C 如何转发typedef';d结构in.h,c,typedef,forward-declaration,C,Typedef,Forward Declaration,我有 预处理器 #define MAX_FILES 15 struct Preprocessor { FILE fileVector[MAX_FILES]; int currentFile; }; typedef struct Preprocessor Prepro; void Prepro_init(Prepro* p) { (*p).currentFile = 0; } #ifndef _PREPROCESSOR_H_ #define _PREPROCESSO
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_
#define MAX_FILES 15
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p);
#endif
<some code>
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
<some code>
那时我意识到我必须将声明与定义分开。所以我创建了预处理器。c:
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
和预处理器。h现在是:
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
这显然不起作用,因为Pr..h不知道预印本类型。我已经试过几种组合,但都不起作用。我找不到解决方案。移动
typedef结构预处理器prepo将文件和c文件中的定义以及prepo_init定义一起编码到头文件。这是转发,我们将为您无问题地申报
预处理器
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_
#define MAX_FILES 15
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p);
#endif
<some code>
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
<some code>
预处理器
#include "Preprocessor.h"
#include <stdio.h>
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
#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>
#包括“Preprocessor.h”
#包括
结构预处理器{
文件fileVector[MAX_FILES];
int-currentFile;
};
无效预印本(预印本*p){
(*p).currentFile=0;
}
如果要隐藏预处理器的定义,只需将其放入头文件:
struct Preprocessor;
typedef struct Preprocessor Prepro;
但更一般地说,您可能还需要头文件中的预处理器定义,以允许其他代码实际使用它。您已经在.c
中输入了.h
中应该包含的内容,反之亦然prepo_init
必须在.c
文件中,并且该文件必须#包括“Preprocessor.h”
交换.h
和.c
文件。在.c
中包含标题
另请参阅一本关于声明、定义和头文件作用的书。YAS:另一种解决方案
预处理器
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_
#define MAX_FILES 15
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p);
#endif
<some code>
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
<some code>
无效预印本(预印本*p){
(*p).currentFile=0;
}
预处理器
#include "Preprocessor.h"
#include <stdio.h>
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
#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>
#定义MAX#U文件15
结构预处理器{
文件fileVector[MAX_FILES];
int-currentFile;
};
typedef结构预处理器prepo;
#定义结构后包括“Preprocessor.h”//include。
{
结构预处理;
初始预处理(p);
..使用p.currentFile。。。。。
…使用其他成员。。。。
}
现在它可以工作了。我想这是你的要求。希望能有帮助
缺点:
结构预处理器的成员必须预先确定。i、 e头文件使用成员currentFile。所以,包含Preprocessor.h的c文件必须有一个类型定义为prepo的结构,并且该结构必须包含一个成员currentFile。(在本例中)
一年前我遇到过同样的问题,当时我正在编写头文件以图形树格式显示用户的Avl树 我建议您遵循Linus[1],不要使用typedef struct
如果您控制库,请执行以下操作:
包含文件
struct Foo;
int foo_get_n(const struct Foo *bar);
在实现文件中
struct Foo
{int n;};
int foo_get_n(const struct Foo *bar)
{return bar->n;}
如果您无法控制库,请执行以下操作:
请维护人员从包含文件中删除这些类型定义
总结
不要使用typedef结构
[1] 这里没有正向声明。请注意,如果您经常使用指向结构的指针,您会发现->
运算符非常有用(*p).currentFile
可以写成p->currentFile
。这是C。我几乎可以肯定你不能在C中这么做。@Erandros不,->
操作符在C中非常有效,C++你是对的,->在C中有效。第一行是不必要的,因为第二个也将声明结构预处理器存在,但没有定义它。@Chris:我知道,但我认为通过显式显示转发声明会使事情更清楚。您还应该提到,prepo类型的唯一可能用途是通过指针。然后,类型的定义将进入.c文件,实现将是唯一了解prepo内部结构的工具。很好的答案。“但更一般地说,您可能还需要头文件中的定义”这一点被夸大了。。。通常,头中所需的只是指向结构的指针,在这种情况下,您的2行解决方案(甚至Chris的1行变体)效果很好。预处理器可能是一个糟糕的名称选择,但这取决于您:)而且文件
通常用指针引用,FILE*
我会将实际的struct
定义放在另一个头文件中,这样您也可以将实现拆分为不同的源文件。这将使您更容易获得所需的函数。@Chris Lutz:也许他希望它是一个不透明的typeOK for.h文件,但现在,c文件不知道什么是预处理程序。@Erandros:按照这里的设置方式,您无法访问预处理程序.c文件之外的currentFile
成员(或任何成员)。如果你需要能够做到这一点,你需要把定义为<代码> Stutt预处理器< /C>在头文件中,并且在你想用它的地方包括头文件。你有什么建议吗?任何一本好书都可以,Struoutru/迈尔斯/萨特。那些是C++书籍,你确定C会是一样的吗?C++更容易转发。