【C语言】——结构体、联合体、枚举、typedef

结构体、联合体、枚举、typedef

    • 1.结构体
      • 1.1 定义结构体struct和初始化
      • 1.2 结构体成员内存对齐详解
      • 1.3 结构作为函数的参数
    • 2 联合体/共用体
      • 2.1 概述
      • 2.2 联合体的指针成员
    • 3. 枚举类型
    • 4 typedef
      • 4.1 概述
      • [4.2 typedef函数指针用法](https://blog.csdn.net/qll125596718/article/details/6891881)

1.结构体

1.1 定义结构体struct和初始化

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include

//struct 结构体名
//{
   
// 结构体成员列表
//	姓名
//	年龄
//  成绩
//}

struct student
{
   
	char name[21];
	int age;
	int score;
	char addr[51];
};//stu = { "张三",18,100,"北京市昌平区北清路22号" };
int main0201()
{
   
	//创建结构体变量
	//结构体类型 结构体变量
	//struct student stu;
	stu.name = "张三";
	//strcpy(stu.name, "张三");
	//stu.age = 18;
	//stu.score = 100;
	//strcpy(stu.addr, "北京市昌平区北清路22号");
	//结构体变量初始化
	/*struct student st = { 0 };
	struct student st1;
	memset(st1, 0, sizeof(st1));*/



	struct student stu = {
    "张三",18,100,"北京市昌平区北清路22号" };
	printf("姓名:%s\n", stu.name);
	printf("年龄:%d\n", stu.age);
	printf("成绩:%d\n", stu.score);
	printf("地址:%s\n", stu.addr);

	return EXIT_SUCCESS;
}

int main0203(void)
{
   
	struct student stu;
	scanf("%s%d%d%s", stu.name, &stu.age, &stu.score, stu.addr);
	printf("姓名:%s\n", stu.name);
	printf("年龄:%d\n", stu.age);
	printf("成绩:%d\n", stu.score);
	printf("地址:%s\n", stu

你可能感兴趣的:(C)