简体   繁体   English

当在另一个文件中声明struct时,如何使用struct作为函数的参数?

[英]How to use struct as argument of a function when the struct is declared in another file?

I'm writing ac project and I'm trying to keep all the struct declarations in the relevant .h files. 我正在编写一个ac项目,并试图将所有结构声明保留在相关的.h文件中。 This allows me to use them in any of the .c files by including the relevant header. 通过包含相关的标头,这使我可以在任何.c文件中使用它们。 I'm also avoiding the inclusion of header files of my project in other header files. 我还避免将我项目的头文件包含在其他头文件中。 I guess this will make the code easier to read and maintain. 我想这将使代码更易于阅读和维护。

My question is how to avoid the inclusion of a header which contains the declaration of a struct in the header file that uses that struct. 我的问题是如何避免在使用该结构的头文件中包含包含结构声明的标头。 Is there any way of doing that?. 有什么办法吗? (I didn't find an answer for this, sorry if it's duplicated). (我没有找到答案,对不起,如果重复)。 Here is a snippet: 这是一个片段:

cmd.c 命令

#include "cmd.h"
#include "params.h"     //This allows usage of struct testPars

void move_parameters(struct testPars *testP) {
...
}

cmd.h 命令

#ifndef CMD_H
#define CMD_H
#include "params.h"     //I want to avoid this include in this file

void move_parameters(struct testPars *testP) ;
#endif

params.h params.h

#ifndef PARAMS_H
#define PARAMS_H

struct testPars {
    char    name[16];
    double  value;
};
#endif

Overall, there's a lot of confusion about proper program design here. 总的来说,这里关于正确的程序设计有很多困惑。

I'm trying to keep all the struct declarations in the relevant .h files. 我正在尝试将所有结构声明保留在相关的.h文件中。 This allows me to use them in any of the .c files by including the relevant header. 通过包含相关的标头,这使我可以在任何.c文件中使用它们。

This is acceptable practice for quick & dirty programming where you have no private encapsulation, or if the structs are of a simple nature. 对于没有私有封装或结构简单的快速而肮脏的编程,这是可接受的做法。

But for professional programs where the structs represent more complex ADTs etc, you should only keep a forward declaration in the header files, and define the struct inside the corresponding c file. 但是对于结构表示更复杂的ADT等的专业程序,您应仅在头文件中保留前向声明,并在相应的c文件中定义结构。 This leads to private encapsulation and modularisation, ie good program design. 这导致私有封装和模块化,即良好的程序设计。 (Search for examples of "opaque type" or "opaque pointers". The formal term for such forward declarations is incomplete type .) (搜索“不透明类型”或“不透明指针”的示例。此类前向声明​​的正式术语是不完整类型 。)

What you are trying to do sounds like the above but completely backwards: expose the definition to the caller, but don't use it yourself. 您尝试做的事情听起来像上面的一样,但完全倒退了:向调用者公开定义,但不要自己使用它。 And obviously you can't use a struct that you haven't included and made visible to your program, because that doesn't make any sense. 显然,您不能使用尚未包含并在程序中可见的结构,因为这没有任何意义。

I'm also avoiding the inclusion of header files of my project in other header files. 我还避免将我项目的头文件包含在其他头文件中。 I guess this will make the code easier to read and maintain. 我想这将使代码更易于阅读和维护。

This is a bad idea. 这是一个坏主意。 Each of your header files is to be regarded as the public documentation of what the corresponding c file does and how it should be used. 您的每个头文件都应被视为有关c文件做什么以及如何使用的公共文档。 While the c file can be regarded as private data, the content is nothing that the caller should concern themselves with. 虽然c文件可以看作是私有数据,但其内容并不是调用者应该关注的任何内容。

An important part of that header file documentation is to list all dependencies that this module has, so that other programmers can quickly take a glance at the top of your header file and immediately tell which files that are needed in order to compile this particular module. 该头文件文档的重要部分是列出该模块具有的所有依赖关系,以便其他程序员可以快速浏览一下头文件的顶部,并立即知道编译该特定模块所需的文件。

Good program design strives for as few dependencies as possible: "loose coupling". 好的程序设计会争取尽可能少的依赖关系:“松耦合”。 Therefore all #includes are regarded as important information that should be put on top of the header file. 因此,所有#includes都被视为重要信息,应放在头文件的顶部。 Header files that aren't used should not be included. 不应包含未使用的头文件。

The c file should not contain any includes except its own header file. c文件除了其自己的头文件外,不应包含任何包含。

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

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