刺激战场
六合彩
贵宾厅
  • 3640阅读
  • 1回复

Question2.1:What's the difference between these two declaration? [复制链接]

上一主题 下一主题
离线那个谁
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-13
— 本帖被 tyrone2497谁 执行加亮操作(2013-07-19) —

Q: What's the difference between these two declarations?
                                   struct x1 { ... };
                                   typedef struct { ... } x2;
A: The first form declares a [url=http://c-faq.com/sx1/index.html#structure tag]structure tag[/url]; the second declares a typedef. The main difference is that the second declaration is of a slightly more abstract type--its users don't necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it:
                           x2 b;
Structures declared with tags, on the other hand, must be defined with the
                             struct x1 a;
form.
(It's also possible to play it both ways:
                    typedef struct x3 { ... } x3;
It's legal, if potentially obscure, to use the same name for both the tag and the typedef, since they live in separate namespaces. See question 1.29.)
离线那个谁

只看该作者 沙发  发表于: 2013-07-13
问题:这两种声明有什么不同?
      struct x1 { ... };
           typedef struct { ... } x2;

回答:第一种形式是声明了一个结构体标签;第二种是声明了一个typedef。主要的不同是第二种声明是一个稍微更抽象的类型,它的使用者不需要知道它是一个结构体,并且当声明实例的时候,关键字struct不会被使用:
      x2   b;
结构体用标签的形式声明之后,另一方面,必须这样定义:
       struct x1 a;
也可以这样同时操作:
        typedef struct x3 { ... } x3;
如果很模糊,对于标签和typedef使用相同的名字是合法的。因为它们是在独立的名字空间的。参考问题1.29。
快速回复
限100 字节
如果您提交过一次失败了,可以用”恢复数据”来恢复帖子内容
 
上一个 下一个