ç的typedef和struct问题 [英] C Typedef and Struct Question

查看:129
本文介绍了ç的typedef和struct问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这两个声明之间的区别,并且是preferred比其他?

  typedef结构IOPORT {
    GPIO_TypeDef *口;
    U16针;
} IOPORT;typedef结构{
    GPIO_TypeDef *口;
    U16针;
} IOPORT;


解决方案

至于哪种风格preferred,我preFER第一样式(名称为在这两个结构标记,并作为一个typedef该结构),只是因为没有比源文件中的几个字符其他缺点。我的IDE的结构片段丢弃在这两个地方的名字,所以我总是有一个结构的标记名称相处一个Typedef的结构名称。

您得到一些小的好处:


  • 结构STRUCTNAME 可以用来指向声明的struct
  • 中的结构
  • 您可以只使用STRUCTNAME用C或C ++ code
  • 脱身
  • 您$​​ P $ pvent一个潜在的(即使在实践中非常罕见)古怪C ++中有与结构名的人正在使用另一个对象而不会出现错误或警告

但是,如果我碰巧在结构definintion我会经常懒洋洋地宣布忽略一个或其他名称手动键入

What's the difference between these two declarations, and is one preferred over the other?

typedef struct IOPORT {  
    GPIO_TypeDef* port;  
    u16           pin;  
} IOPORT;  

typedef struct {  
    GPIO_TypeDef* port;  
    u16           pin;  
} IOPORT;

解决方案

As far as which style is preferred, I prefer the 1st style (with the name in both the struct tag and as a typedef for the struct) simply because there are no drawbacks other than a few more characters in the source file. My IDE's struct snippet drops the name in both places, so I always get a typedef'ed struct name along with a struct tag name.

You get a few small benefits:

  • the "struct STRUCTNAME" can be used to declare pointers to the struct within the struct
  • you can get away with just using STRUCTNAME in either C or C++ code
  • you prevent a potential (even if very rare in practice) oddity in C++ having to do with the struct name being used for another object without error or warning

But, if I happen to manually type in the struct definintion I'll often lazily neglect declaring one or the other name.

这篇关于ç的typedef和struct问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆