Is there a technical reason for the "struct namespace" in C?

In C, most of the code declaring structures will follow this pattern:

/* struct forward-declaration */
typedef struct T T ;

/* struct definition */
typedef struct T
{
   /* etc. */
} T ;

This is so common that most of the developers I talked to did not even know that the code above did two things at the same time (declaration of the structure, then overlaying the structure name into the usual namespace) and simply wrote it out of habit.

In C ++, the problem is mitigated, so you can omit the part that the command is superimposed on. In C # and Java, designers didn't even bother. Therefore, these languages ​​will not help to understand why C does it this way.

So after Oliver Charlworth :

Is there a technical reason to have struct Tother normal identifiers in a separate namespace?

C89/C90:

6.1.2.3

. , . . :

  • [...]

  • , ( struct, union )..

  • [...]

  • . ( ).

C11 (n1570: 6.2.3) .

+4
1

, struct T, T .

, , . , , structless thenlessless:

typedef struct {
  /* interesting fields go here */
} T;

, typedef " ", (, typedef ) (T) struct T. , , struct, , , .

0

All Articles