C语言--(七)结构体(Lellansin)
https://www.bilibili.com/video/av405405
视频地址
3点55过 1小时22分钟 时间是多少?
#include<stdio.h>
#include<windows.h>
int main() {
int hour = 3;
int min = 55;
int pass_hour = 1;
int pass_min =22;
int result_hour, result_min;
result_min = (min + pass_min)%60;
result_hour = hour + pass_hour + (min + pass_min)/60;
printf("time = %d:%d\n", result_hour, result_min);
Sleep(10000);
}
// 结果是 3:55 + 1:22 = 5:17
结构体:声明、定义、初始化
*注意: 不要忘了最后的分号
#include<stdio.h>
struct name { // 声明
int hour; // 成员
int min;
int second;
}; // 不要忘了最后的分号
struct time t; // 定义
struct student { // 声明的同时定义
char name[256];
char sex[2];
int age;
int grade;
} Alan,Tom; //定义
struct student { // 声明的同时定义、初始化
char name[256];
char sex[2];
int age;
int grade;
} Alan = {"alan", "男",16, 12}; // 初始化
结构体作为成员、匿名结构体
// 使用结构体作为成员
struct DATE {
int year;
int mounth;
int day;
}
struct person {
char name[256];
struct DATE birthday;
};
// 匿名结构体
struct {
int number;
char name[256];
char sex;
int age;
int grade;
} Alan,Tom;
实际使用
1.集体初始化:{}集体赋值 多类数组的花括号包着
#include<stdio.h>
#include<windows.h>
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
};
int main() {
struct student alan = {01, "Alan", "男", 16, 12}; // 初始化
char sex[3] = "男"; // 是字符串默认后面有'/0'
printf(alan.name);
Sleep(10000);
return 0;
} // Alan
2.逐个初始化:逐个赋值
#include<stdio.h>
#include<windows.h>
#include<string.h> // 不要忘了
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
};
int main() {
struct student alan;
alan.number = 10;
strcpy(alan.name, "Alan"); // 需要头文件#include<string.h>
// 备注方法不行alan.name = "Alan";长度不同char[5] 和 char[256]
strcpy(alan.sex, "男");
alan.age = 16;
alan.grade = 12;
printf("study number:%d\nname:%s\nsex:%s\nage:%d\ngrade:%d",
alan.number, alan.name, alan.sex, alan.age, alan.grade);
Sleep(10000);
return 0;
}
总结: 声明、定义、调用
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
}; // 声明
struct student alan = {01, "Alan", "男", 16, 12}; // 定义
alan.name; // 调用
多个同时定义
定义一个班的多个学生 alan blan clan
struct student class1[3] = { {1}, {2}, {3}}; // 注意这里是逗号,
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
};
int main() {
struct student class1[3] = { {01, "Alan", "男", 16, 12}, // 注意这里是逗号,
{02, "blan", "男", 16, 12},
{03, "clan", "男", 16, 12}
};
int i;
for(i = 0; i<3; i++) {
printf("study number:%d\nname:%s\nsex:%s\nage:%d\ngrade:%d\n\n", class1[i].number, class1[i].name, class1[i].sex, class1[i].age, class1[i].grade);
}
Sleep(10000);
return 0;
}
结构体指针 : 分量运算符 -> 获取成员
上图左边的代码
(报错不行)
右边的代码
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
} zjc = {12, "zjc", "ab", 25, 2 },*d;
int main() {
d = &zjc;
printf("%d",d->number); // 分量运算符 -> 获取成员
Sleep(10000);
return 0;
}
结构体类型不同问题
应对类型不同问题的处理方式1:
struct student *p = (struct student *)&class1; // 强制让class1变格式为结构体student格式
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
};
int main() {
struct student class1[3] = { {01, "Alan", "男", 16, 12}, // 注意这里是逗号,
{02, "blan", "男", 16, 12},
{03, "clan", "男", 16, 12}
};
struct student *p = (struct student *)&class1; // 强制让class1变格式为结构体student格式
// struct student *p = &class1; // 结构体是自己定义的,系统不知道,不能做优化,让两边的数据类型一样,因而报错。(我的VS还是可以运行出来的)
int i;
for(i = 0; i<3; i++) {
printf("%s\n", p->name);
p++;
}
Sleep(10000);
return 0;
}
/*
class1的结构
struct student class1[3] = { {01, "Alan", "男", 16, 12}, // 注意这里是逗号,
{02, "blan", "男", 16, 12},
{03, "clan", "男", 16, 12}
};
但是*p的结构是这样的
struct student *p = {01, "Alan", "男", 16, 12}
所以不同
*/
//如下程序就没问题:
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct time {
int hour;
int min;
}now = {3,55}, pass = {1,22}, result;
int main() {
// struct time now = {3,55};
// struct time pass = {1,22};
// struct result;
struct time *p = &now; //同一类型的
printf("%d\n", p->hour);
Sleep(10000);
return 0;
}
应对类型不同问题的处理方式2:
struct student *p = &class1[0]; // 此时取地址取到的class1[0]是指向 struct student *这样类型的指针,与p是同一类型
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct student {
int number;
char name[256];
char sex[3];
int age;
int grade;
};
int main() {
struct student class1[3] = { {01, "Alan", "男", 16, 12}, // 注意这里是逗号,
{02, "blan", "男", 16, 12},
{03, "clan", "男", 16, 12}
};
struct student *p = &class1[0]; // 此时取地址取到的是指向 struct student *这样类型的指针,与p是同一类型
// 第一种解决方案:struct student *p = (struct student *)&class1; // 强制让class1变格式为结构体student格式
// struct student *p = &class1; // 结构体是自己定义的,系统不知道,不能做优化,让两边的数据类型一样,因而报错
int i;
for(i = 0; i<3; i++) {
printf("%s\n", p->name);
p++;
}
Sleep(10000);
return 0;
}
结构体方式实现第一个问题:“3点55过 1小时22分钟 时间是多少?”
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct time {
int hour;
int min;
}now = {3,55}, pass = {1,22}, result;
int main() {
// struct time now = {3,55};
// struct time pass = {1,22};
// struct result;
result.hour = now.hour + pass.hour + (now.min + pass.min)/60;
result.min = (now.min + pass.min)%60;
printf("%d:%d\n", result.hour,result.min);
Sleep(10000);
return 0;
}
把计算过程封装起来
#include<stdio.h>
#include<windows.h>
#include<string.h>
struct time { // 结构体声明
int hour;
int min;
}now = {3,55}, pass = {1,22}, result; // 结构体定义、初始化
struct time add(struct time now, struct time pass); // add函数声明
int main() {
// struct time now = {3,55}; // 结构体定义、初始化 也可以在这里
// struct time pass = {1,22};
// struct result;
result = add(now, pass);
printf("%d:%d\n", result.hour, result.min);
Sleep(10000);
return 0;
}
// add函数
struct time add(struct time now, struct time pass) { // 由于是自己定义的,因而需要写上
struct time rel;
rel.hour = now.hour + pass.hour + (now.min + pass.min)/60;
rel.min = (now.min + pass.min)%60;
return rel;
}