Why use different a different tag and typedef for a struct?

后端 未结 5 513
离开以前
离开以前 2020-12-09 20:11

In C code, I\'ve seen the following:

typedef struct SomeStructTag {
    // struct members
} SomeStruct;

I\'m not clear on why this is any d

5条回答
  •  萌比男神i
    2020-12-09 20:48

    There is no reason within the C standard to use different names for tags and types. They cannot interfere with each other because they are in different name spaces and cannot be used in the same places.

    Therefore, the only reason to use different names would be for human psychology. Although other answers have suggested that using different names for tags and types makes it easier to spot them in code, there is never any ambiguity because structure tag names only appear after struct, and only structure tag names appear after struct. More than that, if they are the same, you cannot accidentally use one where you intend the other. So there is no need to use different names.

    The fact that C++ adopted structure tags as class names demonstrates it is unnecessary for there to be any difference.

提交回复
热议问题