结构体
1.为什么要用结构体
数组:一组相同类型的数的集合。
结构体:一组不同类型的数(变量)的集合。(数据量大)
可以用结构体模拟“面向对象”这件事。
2.结构体的定义和使用
#include<stdio.h>
#include<string.h>
//1.如何定义结构体
struct student {
int score;
char name[128];
};//写结构体不要忘记这个分号!
int main()
{
// 类型 变量名 初始值
int a =10;
struct student stu1 = {
98,"JOJO"};//与上述整数格式对比
//2.如何访问结构体:结构体变量名+后缀
printf("结构体:score=%d\n",stu1.score);
printf("结构体:name=%s\n",stu1.name);
struct student stu2;
stu2.score = 100;
//stu2.name="JIJI"; //C语言不允许这么做
strcpy(stu2.name,"JIJI");
printf("结构体:score=%d\n", stu2.score);
printf("结构体:name=%s\n", stu2.name);
}
3.结构体是纸老虎,旧知识新用法,easy
#