C++五种结构体声明方式

结构体

1.正常定义的结构体。
struct Student{         //声明结构体
    char name[5];      //姓名
    int num;            //学号
    int score;        //成绩
};
struct Student stu1;    //定义结构体变量
2.在声明结构体的同时声明变量。
struct Student{         
    char name[5];     
    int num;      
    int score;    
}stu1,stu2;
//这种声明方式等同于下面这段代码。
struct Student{     
    char name[5];  
    int num;        
    int score;    
};
struct Student stu1,stu2;
3.声明一个匿名结构体并创建变量。
struct {         
    char name[5];     
    int num;      
    int score;    
}stu1,stu2;

使用这种方式声明的结构体是匿名的,只能在声明结构体的同时创建变量。

typedef修饰的结构体

typedef的作用是为某种数据类型取==别名==。
1.typedef 修饰的结构体函数
typedef struct Student {         
    char name[5];     
    int num;      
    int score;    
}Stu;			//声明struct Student的别名,Stu。
//此时以下两个式子等价。
struct Student stu1;
Stu stu1;
2.typedef修饰的匿名结构体函数。
typedef struct{         
    char name[5];     
    int num;      
    int score;    
}Stu,Stud;		//声明两个别名。
//虽然我们没有办法使用结构体的名字声明变量。但可以通过Stu与Stud声明变量。
Stu stu1;
Stud stu2;	//不管是使用Stu还是Stud声明,效果都是一样的。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值