struct和typedef struct

分三块来讲述:
1 首先:
      在C中定义一个结构体类型要用typedef:
        typedef struct Student
        {
            int a;
        }Stu;
于是在声明变量的时候就可:Stu stu1;
如果没有typedef就必须用struct Student stu1;来声明
这里的Stu实际上就是struct Student的别名。
另外这里也可以不写Student(于是也不能struct Student stu1;了)
        typedef struct
        {
            int a;
        }Stu;
但在c++里很简单,直接
        struct Student
        {
           int a;
        };
于是就定义了结构体类型Student,声明变量时直接Student stu2;
===========================================
2其次:
在c++中如果用typedef的话,又会造成区别:
       struct   Student  
       {  
          int   a;  
       }stu1;//stu1是一个变量  
       typedef   struct   Student2  
       {  
          int   a;  
        }stu2;//stu2是一个结构体类型  
使用时可以直接访问stu1.a
但是stu2则必须先   stu2 s2;
然后               s2.a=10;
===========================================
3 掌握上面两条就可以了,不过最后我们探讨个没多大关系的问题
如果在c程序中我们写:
       typedef struct  
       {
           int num;
           int age;
       }aaa,bbb,ccc;
这算什么呢?
我个人观察编译器(VC6)的理解,这相当于
       typedef struct  
       {
           int num;
           int age;
       }aaa;
       typedef aaa bbb;
       typedef aaa ccc;
也就是说aaa,bbb,ccc三者都是结构体类型。声明变量时用任何一个都可以,在c++中也是如此。但是你要注意的是这个在c++中如果写掉了typedef关键字,那么aaa,bbb,ccc将是截然不同的三个对象。

posted on 2009-03-06 14:20 小虫虫 阅读(28410) 评论(20)  编辑 收藏 引用 所属分类: C++

评论

# re: struct和typedef struct 2009-09-14 20:16 luna

有帮助,谢谢哈~  回复  更多评论   

# re: struct和typedef struct [未登录] 2009-09-24 23:45 z

解释很好  回复  更多评论   

# re: struct和typedef struct 2009-11-18 14:52 tvrynipl

谢谢了
  回复  更多评论   

# re: struct和typedef struct [未登录] 2009-12-24 15:13

你很强势  回复  更多评论   

# re: struct和typedef struct 2010-05-28 08:51 老五

不错,谢谢  回复  更多评论   

# re: struct和typedef struct 2010-08-04 10:35 hj

谢谢  回复  更多评论   

# re: struct和typedef struct 2010-10-05 12:44

谢谢  回复  更多评论   

# re: struct和typedef struct 2010-10-28 10:41 sd

终于知道区别了,谢谢了  回复  更多评论   

# re: struct和typedef struct 2010-11-18 19:21 sunfish

终于明白了~~谢谢  回复  更多评论   

# re: struct和typedef struct 2010-11-25 16:48

谢谢了。  回复  更多评论   

# re: struct和typedef struct 2010-12-13 15:41 xinqikan

Section 2.6 Typedef Names
http://www.xinqikan.com/2010/1203/ch02lev1sec6.html  回复  更多评论   

# re: struct和typedef struct 2011-04-18 17:23 bodysen

谢谢了啊,有帮助呢。  回复  更多评论   

# re: struct和typedef struct 2011-05-08 17:02 zerolzb

终于在这找到答案了,非常非常地感谢!  回复  更多评论   

# re: struct和typedef struct [未登录] 2011-11-22 19:16 1

谢谢哈!!!  回复  更多评论   

# re: struct和typedef struct 2011-12-25 14:44 Kisa_ss

恩,很不错的总结啊。O(∩_∩)O谢谢。  回复  更多评论   

# re: struct和typedef struct [未登录] 2012-06-02 15:31 cc

很好!  回复  更多评论   

# re: struct和typedef struct 2012-08-21 16:11 百灵

很厉害  回复  更多评论   

# re: struct和typedef struct 2013-03-08 14:26 shu

谢谢 受用了  回复  更多评论   

# re: struct和typedef struct 2013-05-15 08:07 kangear

原来如此,C和C++中不一样。  回复  更多评论   

# re: struct和typedef struct 2013-08-10 10:39 donkey

谢谢。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(5)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜