简体   繁体   English

C结构定义标准

[英]C struct definition standard

I'm programming in C in the last few years and I developed an habit to put the typedef of the struct in the header file, and to leave its definition in the source file, so others can use my files without letting them mess things up. 我在过去几年里用C语言编程,并养成了将结构的typedef放在头文件中的习惯,并将其定义保留在源文件中,这样其他人就可以使用我的文件而不会让他们搞砸了。

// header file
typedef struct s_example EXAMPLE

// source file
struct s_example {
       // something
};

is there something wrong it doing this, should i let others know what i do with my structs? 这样做有什么不对,我应该让其他人知道我对我的结构做了什么吗?

im just asking this because i see a lot of people showing everything in the header file. 我只是问这个,因为我看到很多人在头文件中显示所有内容。

The downside of only having the declaration in the header file is that all other source files that do not include the definition of the struct cannot instantiate an instance of that struct , but only a pointer to it. 只有具有在头文件中声明的缺点是,不包括定义的所有其他源文件struct不能实例化的一个实例struct ,而只是一个指针。 This is because, to those other source files, your struct is an incomplete type . 这是因为,对于其他源文件,您的struct不完整的类型 If that is fine for your application, then there is nothing wrong with just a declaration in the header. 如果这对你的应用程序来说没问题,那么在标题中只有一个声明没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM