struct和typedef struct


1 首先://注意在C和C++里不同
    在C中定义一个结构体类型要用typedef:
    typedef struct Student
    {
    int a;
    }Stu;
    于是在声明变量的时候就可:Stu stu1;
    这里的Stu实际上就是struct Student的别名。Stu==struct Student
  
    但在c++里非常easy。直接
    struct Student
    {
    int a;
    };    
    于是就定义了结构体类型Student,声明变量时直接Student stu2。
======================================================================================


2.其次:
    在c++中假设用typedef的话,又会造成差别:
    struct   Student   
    {   
    int   a;   
    }stu1;//stu1是一个变量  

 
    typedef   struct   Student2   
    {   
    int   a;   
    }stu2;//stu2是一个结构体类型=struct Student  

 
    使用时能够直接訪问stu1.a
    可是stu2则必须先   stu2 s2;
    然后               s2.a=10;
====================================================================================


总结:

C/C++都能够像这样:

 typedef struct Student
    {
    int a;
    }Stu;


这个时候Stu是一个类型。声明变量的时候就可:Stu stu1;

仅仅只是C++有简单的:

struct Student
    {
    int a;
    };  

这个时候Student是一个类型,声明变量: Student stu1


请记住,你可以总结

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4754320.html