结构体数组的定义
类型 变量名[常量表达式]; //定义结构体类型 struct stStudent { int Age; int Level; } //定义结构体变量 struct stStudent st; //定义结构体数组 struct stStudent arr[10]; //或者 stStudent arr[10];
结构体数组初始化
struct stStudent{ int Age; int Level; }; struct stStudent arr[5] = {{0,0},{1,1},{2,2},{3,3},{4,4}}; //或者 arr[0].Age=100; arr[0].Level=100;
结构体成员的使用
格式 :
结构体数组名[下标].成员名 arr[0].Age = 10;
字符串成员的处理
struct stStudent{ int Age; char Name[0x20]; } struct stStudent arr[3] = {{0,"张三"},{1,"李四"},{2,"王五"}}; //读 char buffer[0x20]; strcpy(buffer,arr[0].Name); //写 strcpy(arr[0].Name,"王钢蛋");
结构体数组的内存结构
struct stStudent{ int Age; char Name[0x20]; } struct stStudent arr[3] = {{0,"张三"},{1,"李四"},{2,"王五"}}; int x = arr[0].Age;
结构体 stStudent 的宽度为 8 + 32 = 40
我们观察到 结构体数组在内存中是连续存储的
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论