gpt4 book ai didi

c - 在 C 项目中声明和使用结构的正确方法?

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:32 25 4
gpt4 key购买 nike

我正在构建一个项目,我试图按如下方式组织:

main.c
globals.h
structures.h
FunctionSet1.c, FunctionSet1.h
FunctionSet2.c, FunctionSet2.h
etc.

我想我可以在 structures.h 中定义一个结构类型:

struct type_struct1 {int a,b;}; // define type 'struct type_struct1'

然后在 FunctionSet1.h 中声明一个 function1() 返回一个 type_struct1 类型的结构:

#include "structures.h"
struct type_struct1 function1(); // declare function1() that returns a type 'struct type_struct1'

然后在FunctionSet1.c中编写function1():

#include "FunctionSet1.h"
struct type_struct1 function1() {
struct type_struct1 struct1; // declare struct1 as type 'struct type_struct1'
struct1.a=1;
struct1.b=2;
return struct1;
}

编辑:使用上面更正的代码,编译器返回

306 'struct' tag redefined 'type_struct1' structures.h

文件集是好的做法吗?管理结构的最佳做法是什么?

最佳答案

在您的示例中,您在 structure.h 中声明了一个名为 type_struct 的结构,然后在 FunctionSet1.h 中,您返回的结构是 type_struct,在 .c 中它被称为 struct1。

所以我认为问题在于 struct1 和 type_struct 未被识别,因为它们从未被定义......

但是,您的文件组织良好。

关于c - 在 C 项目中声明和使用结构的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31289169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com