C/C++里面的struct和typedef

今天看到这样的代码

typedef struct _Abc {
    uint64_t unit_id;
    ......
} Abc;

 

开始不理解这个的意思,后来看到这个解释:

https://stackoverflow.com/questions/5558994/naming-convention-when-using-struct-in-c

 

There is no benefit in user code, it's just ugly. In the second example, the HuffCode_ isn't even necessary since the struct type is already named by the typedef.

The only places where this can be useful are:

1. When StructName is already in use, StructName_ gives a different name (but you should really come up with a better name).
2. Identifiers in the C standard library that are not defined by the standard shouldn't conflict with user code identifiers. Therefore, C library writers use the _ prefix in the hopes that users will not use that. Unfortunately, some users do.
3. In very old compilers, it may be useful to give the struct a different name than is used in the typedef. You need both the typedef and the other name if you're building a linked structure (example).

 

posted @ 2017-07-17 19:30  blcblc  阅读(307)  评论(0)    收藏  举报