找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 270|回复: 1

Question2.1:What's the difference between these two declaration?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-13 19:52:38 | 显示全部楼层 |阅读模式

Q: What's the difference between these two declarations?
                                   struct x1 { ... };
                                   typedef struct { ... } x2;
A: The first form declares a structure tag; 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.)

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-13 20:15:50 | 显示全部楼层
问题:这两种声明有什么不同?
      struct x1 { ... };
           typedef struct { ... } x2;

回答:第一种形式是声明了一个结构体标签;第二种是声明了一个typedef。主要的不同是第二种声明是一个稍微更抽象的类型,它的使用者不需要知道它是一个结构体,并且当声明实例的时候,关键字struct不会被使用:
      x2   b;
结构体用标签的形式声明之后,另一方面,必须这样定义:
       struct x1 a;
也可以这样同时操作:
        typedef struct x3 { ... } x3;
如果很模糊,对于标签和typedef使用相同的名字是合法的。因为它们是在独立的名字空间的。参考问题1.29。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 04:42 , Processed in 0.198165 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表