Вопрос

The rule in C against declaring a struct's members more than once seems to me to be the main reason that include guards are necessary. If we have the following in "header.h":

struct s {
    int a;
    char b;
};

and the file "a.h" #include's header.h, then we cannot include both "a.h" and "header.h" as then struct s is defined twice.

My question is, what is the problem with doing this? Why can't multiple identical definitions of structs be allowed? This would remove the need for include guards, and clean up C header files enormously.

The rule in C is that multiple declarations are allowed, but only one definition. For some reason, specifying the members of a struct is called "definition", even though it is not defining a variable or a function.

Это было полезно?

Решение

Re-defining a structure is extremely error-prone. Even in your simple example, if a.h includes a #pragma that adjusts structure packing or alignment before it includes "header.h", then the two definitions may not necessarily be the same any longer. This type of problem would be hard to debug because it would be dependent on the header include order.

In general, there are a number of things that can go wrong when you allow data type re-definition. In exchange, you don't get any real benefits from it other than being able to drop the header guards. Header guards solve the problem and are only a minor level of overhead that doesn't clutter the code unreasonably. Some compilers support a #pragma once (or similar) that acts as an include guard but only requires one line of code.

In my personal opinion, it would be a better idea to guard against multiple inclusion by default and require guard macros only on headers that are designed to be included multiple times (leave the overhead to the use case that is in the vast minority). That's not the way C was originally implemented, though (plus it would make the pre-processor more complicated), so it's extremely unlikely that it will ever change.

Другие советы

Well, it's like C works... It's definition because it provides the actual implementation of the object.

You can't define them twice, because as @Kirilenko said, the definition is providing the implementation of the object. However, I am wondering if your question is also asking something else.

Edit: Change instantiation to definition.

Your definition of s

struct s {
    int a;
    char b;
};

would go into a .c file, or at least you would either extern the struct in a .h file and put the structure instantiation in a .c file, or use typedef in the .h file.

typedef struct s {
    int a;
    char b;
} my_struct_type;

Even if you wanted the struct defined in a .h file, you could prevent its "being defined twice" by bracketing like so

Edit: Removed underscores from macro #ifdef MY_STRUCT_DEFINED struct s { int a; char b; }; #endif

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top