C在没有#include的情况下向头文件告知C文件中的结构



这里有点奇怪。

在一个.c文件中定义了一个结构,在.h文件中又定义了另一个结构的情况下,我如何通过另一个.h或.c文件IE让.h文件知道.c中的结构(反之亦然(:而不能编辑任何一个文件以包含或重定义。

main.c:

typedef struct mystruct
{
int whatever;
} Thing;
#include "header.h"
#include "otherHeaderThatIcanEdit.h"
otherstruct thisMethod()
{
//returns an otherstruct
}

header.h:

typedef struct otherstruct
{
float whatever;
} Stuff;
mystruct thisMethod()
{
//returns a mystruct;
}

在您提供的示例中,实际使用这两个结构的两个函数都可以看到这两种结构。但是你应该把关键字struct放在mystruct和otherstruct之前,比如:

struct otherstruct thisMethod()
{
//returns an otherstruct
}
struct mystruct thisMethod()
{
//returns an otherstruct
}

或者,如果你不能修改这些,那么你可以在可以修改的文件中写下以下两行

typedef Thing mystruct;
typedef Stuff otherstruct;

请注意,这两个函数的名称相同,whish是一个错误。

最新更新