cstructuretypedef

Same name structure with different definition in C


Is it allowed to use same name structure with different definitions in 2 different c files in the same project. For eg.

File1.c

typedef struct
{
    unsigned int unVar;             

} abc;

File2.c

typedef struct
{
    int var;
} abc;

abc is used in both the files. When i compile these file as part of same project there are no errors, but i want to understand whether this is correct usage.


Solution

  • 6.7.2.1 Structure and union specifiers

    1. The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit.

    Types are defined only within a translation unit, a .c file in this case.

    There is no problem with defining two types with the same name in two different translation units.

    However those two types are not compatible unless they follow the rules described in 6.2.7., p1. The types you defined are not compatible.