Should struct definitions go in .h or .c file?

后端 未结 6 1819
死守一世寂寞
死守一世寂寞 2020-12-04 05:26

I\'ve seen both full definitions of structs in headers and just declarations—is there any advantage to one method over the other?

If it makes a differen

相关标签:
6条回答
  • 2020-12-04 05:57

    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.

    But if you are sure it will only be used from one source file, then it really doesn't make any difference.

    0 讨论(0)
  • 2020-12-04 06:02

    I put them into the C file to make it more Object Oriented, see this article.

    0 讨论(0)
  • 2020-12-04 06:09

    Both should result in the same usability, even if one is by linkage, shouldn't they?

    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).

    Compare these versions of api.h and 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;          |
                                          | }                               |
                                          +---------------------------------+
    

    This client of the API works with either version:

    #include "api.h"
    
    void good(struct s *foo)
    {
        api_func(foo, 123);
    }
    

    This one pokes around in the implementation details:

    #include "api.h"
    
    void bad(struct s *foo)
    {
        foo->internal = 123;
    }
    

    which will work with the "definition in header" version, but not with the "definition in implementation" version, as in the latter case the compiler has no visibility of the layout of the structure:

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

    So, the "definition in implementation" version protects against accidental or deliberate misuse of private implementation details.

    0 讨论(0)
  • 2020-12-04 06:16

    If the struct is to be used by other compilation units (.c files) , place it in the header file so you can include that header file wherever it is needed.

    If the struct is only used in one compilation unit (.c file), you place it in that .c file.

    0 讨论(0)
  • 2020-12-04 06:21

    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.

    0 讨论(0)
  • 2020-12-04 06:24

    Generally, I don't think it makes a huge difference whether you put them in the header or source files. However, if you need to access the members of a structure from multiple source files, it is easier to put the structure in a header file and include it from any other files where the structure is needed.

    0 讨论(0)
提交回复
热议问题