English 中文(简体)
结构定义是否应当出现在文件上?
原标题:Should struct definitions go in .h or .c file?

我看到了标题中的<代码>struct的全部定义和只是声明――对一种方法具有任何优势?

如果出现差异,我通常会在<条码>上照样照搬。

typedef struct s s_t;

很显然,这些备选办法是标题档案和类别定义中的申报,或标题档案中的申报和定义。 两者都应带来同样的可使用性,即使通过联系,也应当加以控制?


I see many almost duplicates, e.g. here but no exact matches. Please correct me if I m wrong in this regard.

最佳回答

Private structures for that file should go in the .c file, with a declaration in the .h file if they are used by any functions in the .h .

Public structures should go in the .h file.

问题回答

两者都应带来同样的可使用性,即使通过联系,也应当加以控制?

No, not when you consider other .c files including the same header. If the definition of the structure is not visible to the compiler, the details of that definition cannot be used. A declaration without a definition (e.g. just struct s;) causes the compiler to fail if anything tries to look inside struct s, while still allowing it to e.g. compile struct s *foo; (as long as foo is not later dereferenced).

比较<代码>api.h和api.c:

Definition in header:                 Definition in implementation:
+---------------------------------+   +---------------------------------+
| struct s {                      |   | struct s;                       |
|     int internal;               |   |                                 |
|     int other_stuff;            |   | extern void                     |
| };                              |   | api_func(struct s *foo, int x); |
|                                 |   +---------------------------------+
| extern void                     |   +---------------------------------+
| api_func(struct s *foo, int x); |   | #include "api.h"                |
+---------------------------------+   |                                 |
+---------------------------------+   | struct s {                      |
| #include "api.h"                |   |     int internal;               |
|                                 |   |     int other_stuff;            |
| void                            |   | };                              |
| api_func(struct s *foo, int x)  |   |                                 |
| {                               |   | void                            |
|     foo->internal = x;          |   | api_func(struct s *foo, int x)  |
| }                               |   | {                               |
+---------------------------------+   |     foo->internal = x;          |
                                      | }                               |
                                      +---------------------------------+

该信标的客户有两种版本:

#include "api.h"

void good(struct s *foo)
{
    api_func(foo, 123);
}

在执行过程中,有一个oke子:

#include "api.h"

void bad(struct s *foo)
{
    foo->internal = 123;
}

它将采用“头盔定义”版本,但不采用“执行中的定义”版本,因为在后一种情况下,汇编者对结构的布局不了解:

$ gcc -Wall -c bad.c
bad.c: In function  bad :
bad.c:5: error: dereferencing pointer to incomplete type
$

因此,“执行中的定义”版本保护不受意外或蓄意滥用私人执行细节的影响。

如果要由其他汇编单位(c文档)使用,则将其放在标题档案中,这样,只要需要,你就能够包括该标题档案。

如果单元仅用于一个汇编单位(文档),则将其放在该档案中。

The point is, placing it in a header file allows you to use the structure (or any other definition) from multiple source files, just by including that header file.

但是,如果你确信,它只能从一个来源档案中使用,那么它实际上不会有任何改变。

我将这些文件放在C档案中,使其更加客观,





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签