|
发表于 2020-12-14 19:42:07
|
显示全部楼层
本楼为最佳答案
字符串赋值不能直接name[20]="dingding",应该用strcpy函数
结构体和其他类型基础数据类型一样,例如 int 类型,char 类型 只不过结构体可以做成你想要的数据类型。以方便日后的使用
在定义结构体时不能直接定义成结构体数组,需要先定义结构体,再在程序段中定义结构体数组(类似定义整形数组一样)
提供两种方案
第一种方法
- struct Student
- {
- int num;
- char name[20];
- float score;
- }stu;
- int main(void)
- {
- Student stu[3];
-
- strcpy(stu[0].name,"liyan"),stu[0].score=90.5;
- strcpy(stu[1].name,"dingding"),stu[1].score=239;
- strcpy(stu[2].name,"xianxian"),stu[2].score=344;
- system("pause");
- return 0;
- }
复制代码
第二种方法
- struct Student
- {
- int num;
- char name[20];
- float score;
- }stu;
- int main(void)
- {
- Student stu[3]={{0,"liyan",90.5},{0,"dingding",239},{0,"xianxian",344}};
- system("pause");
- return 0;
- }
复制代码 |
|