结构体笔记

  结构体 
结构体是一种由我们定义的数据类型
 定义语法
 struct+结构体名
 {
     成员列表 
  } ;
 注意:1.名字由关键字struct和结构体名组合而成
 2.a.结构体普通变量通过"."来访问成员
 例如:Tom.num
 b.结构体指针变量通过“->”来访问成员,
 例如:p->id 
 
 1.先声明,后定义

 struct students{
 int num;
 int C_score;
 int ps_score;
 float avg; 
  
}; 
int main() {
    struct students tom;//tom是结构体变量名 
    //student是结构体名  
    return 0;
}


使用:结构体中变量赋初值
    1.struct students tom = {10,90,98,94.5};
    struct students num[3] = .....
    2.tom.num = 10;
  tom.C_score = 95; 
  ...... 
2.在声明变量的同时,定义变量  
 struct students{
 int num;
 int C_score;
 int ps_score;
 float avg; 
  
}tom;    //}nums[3];
3.省略结构体名,直接定义结构体变量 
struct {
 int num;
 int C_score;
 int ps_score;
 float avg; 
  
}tom;     //}nums[3];

struct students{
 int num;
 int C_score[3];
 float avg; 
  
}; 
int main() {
    struct students tom;//tom是结构体变量名 
    //student是结构体名 
    struct students tom{10,{90,80,70},85.5};
     struct students tom{10,90,80,70,85.5};
     printf("序号:%d",tom.num); 
     ......
     return 0;
}

结构体数组 
struct 结构体名 {
    数据类型 成员变量名
    ...... 
}; 

语法: struct 结构体名 数组名[数组长度]
       struct student class2103[6]
    main函数中    struct 数组名 [数组长度] = {初值列表}
                                               {10,20,30},{40,50,60}...... 
                                                 

 typedef的使用(type define类型定义) 
 
注意:他不是用来创造类型 ,而是给已有的类型取一个别名
 如何知道那个是类型 
 int a;
 int a[10];
 struct student stu;
 规则:将名字去掉,剩下就是我类型
 int
 int[10]
 struct student
 语法:typedef 已有的类型 别名;
 例如:typedef int TYPE
 //TYPE与int等价,如TYPE a;相当于int a; 
 typedef struct student {
     int num;
     int age;
 }STU; //STU是 struct student的别名
   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值