结构体
定义:用于存储不同的数据类型,存储在同一块内存空间里面
1、关键字 struct
2、标签 结构体名称
3、成员
例如:
struct student
{
char name[20];
char sex;
int age;
float grade;
};
结构体后面不要忘记加分号
结构体至少需要一个标签,证明身份
结构体的使用
1、.访问指针
2、→访问
#include <stdio.h>
#include <string.h>
struct student
{
char name[20];
char sex[10];
int age;
float grade;
};
int main()
{
struct student stu={"李四","男",18,59.9};
struct student stu2;
strcpy(stu2.name,"赵四");
strcpy(stu2.sex,"女");
stu2.age=19;
stu2.grade=78;
struct student stu3={.name="张三".age=17,.sex="男",.grade=90};
struct student stu4={name:"吴强",age:21,sex:"男",grade:99};
printf("姓名是:%s\n",stu.name);
printf("性别是:%s\n",stu.name);
printf("年龄是:%d\n",stu.name);
printf("成绩是:%f\n",stu.name);
printf("姓名是:%s\n",stu2.name);
printf("性别是:%s\n",stu2.name);
printf("年龄是:%d\n",stu2.name);
printf("成绩是:%f\n",stu2.name);
printf("姓名是:%s\n",stu3.name);
printf("性别是:%s\n",stu3.name);
printf("年龄是:%d\n",stu3.name);
printf("成绩是:%f\n",stu3.name);
printf("姓名是:%s\n",stu4.name);
printf("性别是:%s\n",stu4.name);
printf("年龄是:%d\n",stu4.name);
printf("成绩是:%f\n",stu4.name);
return 0;
}
字符串拷贝函数
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210108092740885.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210108093151814.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BpZWRz,size_16,color_FFFFFF,t_70)
字符串比较函数 strcmp和strncmp
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210108094727491.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210108095029779.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BpZWRz,size_16,color_FFFFFF,t_70)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)