结构体
不同类型数据的集合
数组是相同类型数据的集合
结构体定义语法
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//定义结构体
struct student {
char name[200];
unsigned int age;
char tel[16];
float scores[3];
char sex;
} stu;//初始化结构体方法四
int main(void)
{
//初始化结构体方法一:
//struct student stu = { "zhangsan",18,"18112345678",100,200,300,'M'};
//初始化结构体方法二:
//struct student stu = { .age = 18, .name="zhangsan",.tel="18112345678",.scores[0]=100,.scores[1]=200,.scores[2]=300,.sex='M'};
//初始化结构体方法三:
//struct student stu;
strcpy(stu.name, "谢允");
stu.age = 18;
strcpy(stu.tel, "18112345678");
stu.scores[0] = 100;
stu.scores[1] = 100;
stu.scores[2] = 100;
stu.sex = 'M';
printf("姓名:%s\n", stu.name);
printf("年龄:%u\n", stu.age);
printf("电话:%s\n", stu.tel);
printf("成绩:%.1f\t%.1f\t%.1f\n", stu.scores[0],stu.scores[1],stu.scores[2]);
printf("性别:%s\n", stu.sex == 'M' ? "男" : "女");
return 0;
}
结构体大小和内存存储结构
//结构体需要根据数据类型进行内存对齐,按最大数据类型分割内存区域
struct student {
char name[20];//20
unsigned int age;//4
char tel[15];//15
float scores[3];//12
cha