在 C 项目中声明和使用结构的正确方法?
Proper way to declare and use structures in C project?
我正在构建一个项目,我试图按如下方式组织:
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;
}
编辑:使用上面更正的代码,编译器 returns
306 'struct' tag redefined 'type_struct1' structures.h
文件设置是好的做法吗?
管理结构的最佳做法是什么?
在您的示例中,您在 structure.h 中声明了一个名为 type_struct 的结构,然后在 FunctionSet1.h 中,您返回的结构是 type_struct,并且在 . c 它被称为 struct1.
所以我认为问题在于 struct1 和 type_struct 未被识别,因为它们从未被定义过...
但是,您的文件组织良好。
首先,您必须在 file.h
中声明结构(您可以使用 typedef
创建别名)
typedef struct Books
{
char title[50];
int book_id;
} books;
然后,您必须将 file.h
包含在 file.c
中,并像这样声明您的变量
#include "file.h"
int main()
{
books book1;
book1.title = "Harry Potter";
book1.book_id = 54;
}
或者如果你没有使用 typedef
#include "file.h"
int main()
{
struct Books book1;
book1.title = "Harry Potter";
book1.book_id = 54;
}
你的总体结构看起来不错。正如 zenith 提到的,您需要做的一件事是将 include guards 放入您的 header 文件中。那是一组#define,确保header 的内容不会在给定文件中多次包含。例如:
structures.h:
#ifndef STRUCTURES_H
#define STRUCTURES_H
struct type_struct1{
int a,b;
};
...
// more structs
...
#endif
FunctionSet1.h:
#ifndef FUNCTION_SET_1_H
#define FUNCTION_SET_1_H
#include "structures.h"
struct type_struct1 function1();
...
// more functions in FucntionSet1.c
...
#endif
main.c:
#inlcude <stdio.h>
#include "structures.h"
#include "FunctionSet1.h"
int main(void)
{
struct type_struct1 struct1;
struct1 = function1();
return 0;
}
这里,main.c包括structures.h和FunctionSet1.h,但FunctionSet1.h也包括structures.h。如果没有包含防护,structures.h 的内容将在预处理器完成后在生成的文件中出现两次。这可能就是您收到 "tag redefined" 错误的原因。
include guards 可以防止此类错误的发生。这样您就不必担心是否包含了特定的 header 文件。如果您正在编写库,这一点尤其重要,因为其他用户可能不知道您的 header 文件之间的关系。
谢谢大家
我又看了一遍你说的,发现上面的代码现在是正确的。
我报告的错误是测试以下 main.c
#include "structures.h"
#include "FunctionSet1.h"
void main() {
struct type_struct1 struct2;
struct2=function1();
}
其中又包含了structures.h,从而导致错误。删除包含消除了错误。
我现在将研究头部防护以避免此类问题。
再次感谢。
我正在构建一个项目,我试图按如下方式组织:
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;
}
编辑:使用上面更正的代码,编译器 returns
306 'struct' tag redefined 'type_struct1' structures.h
文件设置是好的做法吗? 管理结构的最佳做法是什么?
在您的示例中,您在 structure.h 中声明了一个名为 type_struct 的结构,然后在 FunctionSet1.h 中,您返回的结构是 type_struct,并且在 . c 它被称为 struct1.
所以我认为问题在于 struct1 和 type_struct 未被识别,因为它们从未被定义过...
但是,您的文件组织良好。
首先,您必须在 file.h
中声明结构(您可以使用 typedef
创建别名)
typedef struct Books
{
char title[50];
int book_id;
} books;
然后,您必须将 file.h
包含在 file.c
中,并像这样声明您的变量
#include "file.h"
int main()
{
books book1;
book1.title = "Harry Potter";
book1.book_id = 54;
}
或者如果你没有使用 typedef
#include "file.h"
int main()
{
struct Books book1;
book1.title = "Harry Potter";
book1.book_id = 54;
}
你的总体结构看起来不错。正如 zenith 提到的,您需要做的一件事是将 include guards 放入您的 header 文件中。那是一组#define,确保header 的内容不会在给定文件中多次包含。例如:
structures.h:
#ifndef STRUCTURES_H
#define STRUCTURES_H
struct type_struct1{
int a,b;
};
...
// more structs
...
#endif
FunctionSet1.h:
#ifndef FUNCTION_SET_1_H
#define FUNCTION_SET_1_H
#include "structures.h"
struct type_struct1 function1();
...
// more functions in FucntionSet1.c
...
#endif
main.c:
#inlcude <stdio.h>
#include "structures.h"
#include "FunctionSet1.h"
int main(void)
{
struct type_struct1 struct1;
struct1 = function1();
return 0;
}
这里,main.c包括structures.h和FunctionSet1.h,但FunctionSet1.h也包括structures.h。如果没有包含防护,structures.h 的内容将在预处理器完成后在生成的文件中出现两次。这可能就是您收到 "tag redefined" 错误的原因。
include guards 可以防止此类错误的发生。这样您就不必担心是否包含了特定的 header 文件。如果您正在编写库,这一点尤其重要,因为其他用户可能不知道您的 header 文件之间的关系。
谢谢大家
我又看了一遍你说的,发现上面的代码现在是正确的。 我报告的错误是测试以下 main.c
#include "structures.h"
#include "FunctionSet1.h"
void main() {
struct type_struct1 struct2;
struct2=function1();
}
其中又包含了structures.h,从而导致错误。删除包含消除了错误。
我现在将研究头部防护以避免此类问题。
再次感谢。