4.9
1 指针 数组和函数
关于指针和函数以及指针和数组的传递
实参的传递都是将地址传给函数,普通函数即传递地址符 & 数据;数组函数传递 数组名即可,数组名即代表数组的首地址;
函数的创建,普通数据创建 返回值类型 函数名(数据类型* 指针 )
传递数组的函数创建 返回值类型 函数名(数据类型* 数组名 数据类型 数组长度)
#include<iostream>
using namespace std;
void bubblesort1(int* arr, int len)//函数的形参设置 int* arr 数组指针
{
//冒泡排序升序函数
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void printf1(int* p, int len)
{
cout << "排序后的数组:" << endl;
for (int i = 0; i < len; i++)
{
cout<< *p << " ";
p++;
}
}
int main()
{
//创建一个数组
int arr[] = { 2,6,8,3,0,43,1,7,9 };
int len = sizeof(arr) / sizeof(arr[0]);
int* p = arr;
cout << "排序前的数组:" << endl;
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//冒泡排序
bubblesort1(arr,len);//将数组的数组名即代表数组的首地址传递给函数
//打印函数
printf1(arr, len);
cout << endl;
system("pause");
return 0;
}
2 结构体的创建及命名
#include<iostream>
using namespace std;
struct student1
{
string name;
int age;
int score;
};
int main()
{
//1、结构体的定义及使用
//1.1结构体实质为自己创建的一个数据类型包含不同数据类型的集合,前面需要加上一个struct让计算机提前知道
//1.2 所以结构体创建的语法 struct 结构体名称/数据类型 {};
struct student1 s1;
//1.2.1结构体命名1
s1.name = "张三";
s1.age = 19;
s1.score = 99;
//结构体命名2
student1 s2 = { "里斯",22,96 };
//结构体命名3,结构体创建时就命名
cout << "姓名:" << s2.name << endl;
cout << "年龄:" << s2.age << endl;
cout << "分数:" << s2.score << endl;
system("pause");
return 0;
}
3 结构体数组
#include<iostream>
using namespace std;
//创建结构体数组
struct student2
{
string name;
int age;
int score;
};
int main()
{
srand((unsigned int)time(NULL));
//创建结构体数组
student2 s1arr[] =
{
{"张三",17,88},
{"里斯",22,91},
{"万物",20,99}
};
int num = sizeof(s1arr) / sizeof(s1arr[0]);
//打印结构体数组
for (int i = 0; i < num; i++)
{
cout << "第" << i+1 << "的姓名" << s1arr[i].name << "\t";
cout << "第" << i+1 << "的年龄" << s1arr[i].age << "\t";
cout << "第" << i+1 << "的分数" << s1arr[i].score << "\t";
cout << endl;
}
//给结构体赋值
for (int i = 0; i < num; i++)
{
int age = rand() % 20;
int score = rand() % 99;
s1arr[i].age = age;
s1arr[i].score = score;
}
for (int i = 0; i < num; i++)
{
cout << "第" << i + 1 << "的姓名" << s1arr[i].name << "\t";
cout << "第" << i + 1 << "的年龄" << s1arr[i].age << "\t";
cout << "第" << i + 1 << "的分数" << s1arr[i].score << "\t";
cout << endl;
}
system("pause");
return 0;
}
4 结构体指针
结构体数组与指针的应用类似于数组与指针的应用
创建一个结构体数组,创建一个指针变量,指针指向数组名即为数组首地址
用指针遍历结构体数组或是在用指针赋值数组的时候采用循环,num元素个数为数组的
for(int i=0; i<num;i++)
{
指针打印或赋值操作;
指针++;
}
用循环限制指针的指向同时满足指针的移动
后续进行指针操作前,一定要先进行***指针的初始化***;
#include<iostream>
using namespace std;
//结构体创建
struct student3
{
string name;
int age;
int score;
};
int main()
{
//结构体与指针
//1.1、定义结构体
student3 s1;
s1.age = 19;
s1.name = "赵六";
s1.score = 99;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//1.2、创建指针
student3* p;
p = &s1;
//1.3、用指针对结构体赋值
p->age = 80;
p->score = 90;
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
//2.1、创建结构体数组
student3 arr[] =
{
{"张三",17,88},
{"里斯",22,91},
{"万物",20,99}
};
int num = sizeof(arr) / sizeof(arr[0]);
//2.2、创建指针
student3* p2;
p2 = arr;
for (int j = 0; j < num; j++)
{
cout << "姓名:" << p2->name << " 年龄:" << p2->age << " 分数:" << p2->score << endl;
p2++;
}
p2 = arr;//*****指针初始化******
//2.3、用指针对结构体数组赋值
for (int i = 0; i < num; i++)
{
p2->age = 33;
p2->score = 89;
p2++;
}
p2 = arr;//*****指针初始化******
for (int j = 0; j < num; j++)
{
cout << "姓名:" << p2->name << " 年龄:" << p2->age << " 分数:" << p2->score << endl;
p2++;
}
system("pause");
return 0;
}
5 结构体嵌套结构体
#include<iostream>
using namespace std;
//创建一个学生子结构体
struct student4
{
string sname;
int age;
int score;
};
//创建一个老师主结构体
struct teacher1
{
string tname;
int id;
student4 stu;
};
int main()
{
//结构体的嵌套
student4 s1 = { "万物",23,99 };
teacher1 t1 = { "老王",1100,s1 };
//打印
cout << "老师的姓名:" << t1.tname << " 老师的编号:" << t1.id << endl;
cout << "老师学生得姓名:" << t1.stu.sname << " 老师学生的年龄:" << t1.stu.age
<< " 老师的学生的成绩:" << t1.stu.score << endl;
//用指针操作
student4* p1 = &s1;
teacher1* p2 = &t1;
cout << "老师的姓名:" << p2->tname << " 老师的编号:" << p2->id << endl;
cout << "老师学生得姓名:" << p2->stu.sname << " 老师学生的年龄:" << p2->stu.age
<< " 老师的学生的成绩:" << p2->stu.score << endl;
system("pause");
return 0;
}
遗留问题 结构体嵌套时的指针表示 t1->stu->sname???
stu为主结构体的一个单独数据类型,没有定义指针指向它
6 结构体做函数参数
首先是注意值传递与地址传递中对结构体的影响
其次根据数组指针与函数以及结构体指针与函数的例子体会函数传参的异同点
返回值类型 函数名(数据类型* 指针);
数组传递中还要传入数组的元素个数
遗留问题 结构体数组指针与函数的传递
数组做参数传递,实参中只能以数组名来表示,只能是指针的传递
#include<iostream>
using namespace std;
//创建一个学生结构体
struct student5
{
string name;
int age;
int score;
};
//值传递函数
void printf1(struct student5 s11)
{
s11.score = 67;
cout << "子函数中学生的姓名:" << s11.name << " 学生的年龄:" << s11.age << " 学生的分数:" << s11.score << endl;
}
//地址传递函数
void printf2(struct student5* p0)
{
p0->score = 45;
cout << "子函数中学生的姓名:" << p0->name << " 学生的年龄:" << p0->age << " 学生的分数:" << p0->score << endl;
}
int main()
{
//结构体做函数参数——值传递
//1、将学生作为一个参数传入到函数中
//1.1定义结构体
student5 s1;
s1.name = "张三";
s1.age = 25;
s1.score = 100;
//打印
cout << "值传递前 :\t" << "学生的姓名:" << s1.name << " 学生的年龄:" << s1.age << " 学生的分数:" << s1.score << endl;
//1.2将结构体传入到函数中
printf1(s1);
cout << "值传递后 :\t" << "学生的姓名:" << s1.name << " 学生的年龄:" << s1.age << " 学生的分数:" << s1.score << endl;
//地址传递
//2.1
//2.2创建指针变量
student5* p = &s1;
cout << "地址传递前 :\t" << "学生的姓名:" << p->name << " 学生的年龄:" << p->age << " 学生的分数:" << p->score << endl;
//地址传递
printf2(&s1);
cout << "地址传递后 :\t" << "学生的姓名:" << p->name << " 学生的年龄:" << p->age << " 学生的分数:" << p->score << endl;
system("pause");
return 0;
}
7 结构体中的const使用,但存在地址传递时,函数中加入const可以防止对结构体数据的误操作
否则在函数中对结构体的数据进行修改会在main函数中也相应修改
#include<iostream>
using namespace std;
//创建一个结构体
struct student6
{
string name;
int age;
int score;
};
//创建一个打印函数
void printf3(const student6* p)
{
//p->age = 99; 不可修改
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main()
{
//定义一个结构体
student6 s;
s.name = "万物";
s.age = 17;
s.score = 99;
//地址传递
printf3(&s);
system("pause");
return 0;
}