相关疑难解决方法(0)

如何在c中的多个文件使用的头中声明一个结构?

如果我有一个带结构的source.c文件:

struct a { 
    int i;
    struct b {
        int j;
    }
};
Run Code Online (Sandbox Code Playgroud)

如何在另一个文件(即func.c)中使用此结构?

我应该创建一个新的头文件,在那里声明结构并包含该头func.c

或者我应该在头文件中定义了整个结构,包括在两个source.cfunc.c?如何extern在两个文件中声明结构?

typedef应该吗?如果是这样,怎么样?

c structure header file

112
推荐指数
3
解决办法
25万
查看次数

undefined C struct forward声明

我有一个头文件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)

非常感谢任何建议,

c struct declaration forward

20
推荐指数
3
解决办法
4万
查看次数

标签 统计

c ×2

declaration ×1

file ×1

forward ×1

header ×1

struct ×1

structure ×1