结构体

格式:
struct 结构体名 {
        数据类型 变量1;
        数据类型 变量2;
数据类型 变量3 ;
        ... ...
}
例如:
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;      
  4. };  

三、结构体变量的定义

1、先定义结构体,再定义变量
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. };  
  5. struct Student stu;  
2、定义结构体的同时定义变量
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. } stu;  
3、直接定义结构体变量
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct {  
  2.     int age;  
  3.     charchar *name;  
  4. } stu;  

四、结构体的初始化

1、声明变量的同时初始化
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. };  
  5. struct Student stu = {22"hello"};  
2、先声明变量,再逐一初始化
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student stu;  
  2. stu.age = 22;  
  3. stu.name = "hello";  
3、定义结构体的同时进行变量定义和初始化
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. } stu = {22"hello"};  
4、非顺序初始化
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student stu = {.name = "hello".age = 22};  
5、注意:如果没有初始化结构体,所有变量会自动的有默认值

五、访问结构体

1、结构体变量是基本数据类型
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int age = stu.age;  
  2. charchar *name = stu.name;  
2、结构体变量是结构体
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Date {  
  2.     int year;  
  3.     int month;  
  4.     int day;  
  5. };  
  6.   
  7. struct Student {  
  8.     int age;  
  9.     charchar *name;  
  10.     struct Date birthday;  
  11. };  
  12.   
  13. struct Student stu = {22"hello", {19991010}};  
  14. int year = stu.birthday.year;  
  15. int month = stu.birthday.month;  
  16. int day = stu.birthday.day;  
3、相同结构体类型变量间可以整体赋值
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student stu1 = {23"hello"};  
  2. struct Student stu2 = stu1;     // 把结构体变量stu1的值对应的赋值给stu2,所以stu2 = {23, "hello"}  

六、结构体数组

1、三种定义方式:
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 方式一  
  2. struct Student {  
  3.     int age;  
  4.     charchar *name;  
  5. } stus[3];  
  6.   
  7. // 方式二  
  8. struct Student {  
  9.     int age;  
  10.     charchar *name;  
  11. };  
  12. struct Student stu[3];  
  13.   
  14. // 方式三  
  15. struct {  
  16.     int age;  
  17.     charchar *name;  
  18. } stus[3];  
2、初始化
和数组的初始化一样,可以参照 数组笔记
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. stus = {{12"hello"}, {23"world"}, {24"welcome"}}  

七、结构体指针

1、格式
struct 结构体名 *指针名;
2、访问结构体的方式增加了
之前访问结构体的方式是
结构体变量名.成员变量名
现在增加了
(*指针名).成员变量名
指针名->成员变量名

3、例子
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. };  
  5.   
  6. struct Student stu = {23"hello"};  
  7. struct Student *p = &stu;  
  8.   
  9. printf("%d, %s\n", stu.age, stu.name);  
  10. printf("%d, %s\n", (*p).age, (*p).name);  
  11. printf("%d, %s\n", p->age, p->name);  

八、结构体与函数

1、结构体作为函数参数
结构体实参会把成员变量值对应的赋值给函数结构体参数对应的变量值,改变函数结构体参数不会影响到实参。
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. };  
  5.   
  6. void test(struct Student s)  
  7. {  
  8.     // 对结构体s的操作不会影响到stu  
  9.     s.age = 10;  
  10.     s.name = "world";  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.     struct Student stu = {23"hello"};  
  16.     test(stu);  
  17.     printf("%d, %s\n", stu.age, stu.name);  
  18.       
  19.     return 0;  
  20. }  
输出:

23, hello

2、结构体指针作为函数参数
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct Student {  
  2.     int age;  
  3.     charchar *name;  
  4. };  
  5.   
  6. void test(struct Student * s)  
  7. {  
  8.     // 对结构体s的操作会影响到stu  
  9.     s->age = 10;  
  10.     s->name = "world";  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.     struct Student stu = {23"hello"};  
  16.     test(&stu);  
  17.     printf("%d, %s\n", stu.age, stu.name);  
  18.       
  19.     return 0;  
  20. }  
输出:

10, world

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

→嵌入式Linux开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值