如果我有一个带结构的source.c文件:
struct a {
int i;
struct b {
int j;
}
};
Run Code Online (Sandbox Code Playgroud)
如何在另一个文件(即func.c)中使用此结构?
我应该创建一个新的头文件,在那里声明结构并包含该头func.c?
或者我应该在头文件中定义了整个结构,包括在两个source.c和func.c?如何extern在两个文件中声明结构?
我typedef应该吗?如果是这样,怎么样?
我有一个头文件port.h,port.c和我的main.c
我收到以下错误:'ports'使用未定义的struct'port_t'
我想,因为我在.h文件中声明了结构,并且.c文件中的实际结构是可以的.
我需要有前向声明,因为我想在port.c文件中隐藏一些数据.
在我的port.h中,我有以下内容:
/* port.h */
struct port_t;
Run Code Online (Sandbox Code Playgroud)
port.c:
/* port.c */
#include "port.h"
struct port_t
{
unsigned int port_id;
char name;
};
Run Code Online (Sandbox Code Playgroud)
main.c中:
/* main.c */
#include <stdio.h>
#include "port.h"
int main(void)
{
struct port_t ports;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何建议,