奇牛学院
---------------------------------------------------------------------------------------------------------------------------------
1.为什么要使用结构体?
需要定义多个重复大量的变量时,一个个的定义显然是不方便的,而且也不现实,此时结构体运用起来就很方便快捷。例如学生函数(学号,姓名,性别,年龄。。。)
--------------------------------------------------------------------------------------------------------------------------------
2.什么是结构?
结构分很多种,例如整型结构(int),单精度浮点类型(float)。。。这些都是公认的定义好的类型结构,而结构体类型是自己定义的类型,使用多个基本数据结构,或其他类型,组合成一种全新的结构供程序员使用。
---------------------------------------------------------------------------------------------------------------------------------
3.结构的定义
要以struct开头,以分号结尾,{ 定义的变量 }
struct 结构名{
成员类型 成员名;
成员类型 成员名;
};
----------------------------------------------------------------------------------------------------------------------------
4.结构体的使用格式
结构体变量.成员名
中间用 . 调用成员名
----------------------------------------------------------------------------------------------------------------------------
5.结构体的初始化
5.1 结构体的整体赋值和输出
#include <iostream>
#include <Windows.h>
using namespace std;
struct student{
char name[16];
int age;
char sex[4];
};
int main(void){
//直接赋值
struct student s1 = { "rock", 16, "男" };
//输出
cout<< "姓名: "<< s1.name << endl << "年龄: "<< s1.age <<endl;
cout<< "性别: "<< s1.sex <<endl;
system("pause");
return 0;
}
5.2 结构体单个赋值和输出
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
struct student{
char name[16];
int age;
char sex[4];
};
int main(void){
//单个赋值
struct student s2;
strcpy(s2.name,"rock");
s2.age = 16;
strcpy(s2.sex,"男");
//输出
cout<< "姓名: "<< s2.name << endl << "年龄: "<< s2.age <<endl;
cout<< "性别: "<< s2.sex <<endl;
system("pause");
return 0;
}
5.3 结构体件嵌套结构体(赋值和输出)
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
struct student{
char name[16];
int age;
char sex[4];
};
struct _class{
struct student rock;
struct student jack;
};
int main(void){
//整体初始化
//struct _class s3 = {{"Rock",18,"男"},{"jack",16,"男"}};
struct _class s3;
//单个赋值
strcpy(s3.rock.name,"Rock");
s3.rock.age = 18;
strcpy(s3.rock.sex,"男");
//输出
cout<< "姓名: "<< s3.rock.name << endl << "年龄: "<< s3.rock.age <<endl;
cout<< "性别: "<< s3.rock.sex <<endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
6.结构体的使用
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
struct student{
char name[16];
int age;
char sex[4];
};
int main(void){
struct student s1;
cout<<"请输入学生的姓名:";
cin>>s1.name;
cout<<"请输入学生的年龄:";
cin>>s1.age;
cout<<"请输入学生的性别:";
cin>>s1.sex;
//输出
cout<< "姓名: "<< s1.name << endl << "年龄: "<< s1.age<<endl;
cout<< "性别: "<< s1.sex <<endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
7.结构体数组
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
struct student{
char name[16];
int age;
char sex[4];
};
int main(void){
struct student s1[2];
cout<<"请输入学生的姓名:";
cin>>s1[0].name;
cout<<"请输入学生的年龄:";
cin>>s1[0].age;
cout<<"请输入学生的性别:";
cin>>s1[0].sex;
//输出
cout<< "姓名: "<< s1[0].name << endl << "年龄: "<< s1[0].age<<endl;
cout<< "性别: "<< s1[0].sex <<endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
8.结构体指针
#include <iostream>
#include <Windows.h>
#include <string.h>
using namespace std;
struct student{
char name[16];
int age;
char sex[4];
};
int main(void){
struct student s1[2];
struct student *p = s1;
cout<<"请输入学生的姓名:";
cin>>s1[0].name;
cout<<"请输入学生的年龄:";
cin>>s1[0].age;
cout<<"请输入学生的性别:";
cin>>s1[0].sex;
printf("-----------------------------------------------------------------------------------------\n");
//输出
cout<< "姓名: "<< s1[0].name << endl << "年龄: "<< s1[0].age<<endl;
cout<< "性别: "<< s1[0].sex <<endl;
printf("-----------------------------------------------------------------------------------------\n");
//结构体指针的2种输出格式输出
//1.解引输出
cout<< "姓名: "<< (*p).name << endl << "年龄: "<< (*p).age<<endl;
cout<< "性别: "<< (*p).sex <<endl;
printf("-----------------------------------------------------------------------------------------\n");
//2. ->输出
cout<< "姓名: "<< p->name << endl << "年龄: "<< p->age <<endl;
cout<< "性别: "<< p->sex <<endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
9.结构体传值
9.1结构体的值传递(结果不变)
#include <iostream>
#include <string.h>
#include <Windows.h>
using namespace std;
struct add_salary{
char name[24];
int salary;
};
//结构体的值传递
void add_salary1(struct add_salary p, int num){
p.salary+=num;
}
int main(void){
struct add_salary rock;
strcpy(rock.name,"Rock");
rock.salary = 10000;
add_salary1(rock,5000);
printf("姓名: %s\n工资: %d\n",rock.name,rock.salary);//工资仍为10000
system("pause");
return 0;
}
9.2 结构体指针的传递
#include <iostream>
#include <string.h>
#include <Windows.h>
using namespace std;
struct add_salary{
char name[24];
int salary;
};
//结构体指针的传递
void add_salary2(struct add_salary *p, int num){
if(!p) return ;
p->salary+=num;
}
int main(void){
struct add_salary rock;
strcpy(rock.name,"Rock");
rock.salary = 10000;
//add_salary1(rock,5000);
add_salary2(&rock,5000);
printf("姓名: %s\n工资: %d\n",rock.name,rock.salary);//salary=15000;
system("pause");
return 0;
}
9.3结构体的引用
--------------------------------------------------------------------------------------------------------------------------------