指针类型 指针名;
(指针类型指int *
,float *
等等)*
来操作指针所指向的内存#include
using namespace std;
int main() {
int a = 12;
// 定义指针: 数据类型 * 指针名;
int * p;
p = &a; // &为取地址符号,可以取到a的地址
cout << "&a = " << &a << endl;
cout << " p = " << p << endl; // 二者打印的内容相同,均为变量a的地址
// 使用指针
*p = 12345; // *代表解引用,可以通过它找到指针指向的内存中的数据
cout << "*p = " << *p << endl;
cout << " a = " << a << endl; // 修改*p的值也间接的修改了变量a的值
system("pause");
return 0;
}
int a = 99;
int* p = &a;
// 具体输出和操作系统有关
cout << "sizeof(int *) = " << sizeof(p) << endl;
cout << "sizeof(bool *) = " << sizeof(bool *) << endl;
cout << "sizeof(char *) = " << sizeof(char *) << endl;
cout << "sizeof(double *) = " << sizeof(double *) << endl;
cout << "sizeof(long long *) = " << sizeof(long long *) << endl;
注意:空指针指向的内存是不可以被访问的!!!
(0~255之间的内存编号是系统占用的,不能被访问)#include
using namespace std;
int main() {
int* p = NULL;
int a = *p;
system("pause");
return 0;
}
int * p = (int *)0x1100;
// 这里会报访问权限异常,因为这个地址就不是由你本人申请的!
int c = *p;
空指针与野指针,都不是我们申请的空间,请不要随意的访问它!!!!
int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int* p = arr; // 将这个指针指向数组的首地址
cout << "指针访问第一个元素:" << *p << endl; // 输出0
/*
// 如果需要用指针来访问第二个元素,需要让指针指向后移4个字节
p++; // 自增已经可以让指针向后移动4字节
cout << "第二个元素: " << *p << endl; // 输出1
*/
// 利用指针遍历数组元素
for (int i = 0; i < size(arr); i++) {
cout << "arr[" << i << "] = " << *p << endl;
cout << "p = " << p << endl;
cout << "&arr[" << i << "] = " << &arr[i] << endl;
p++;
}
#include
using namespace std;
void swap01(int* a, int* b);
// 值传递,并不会改变传入的形参的值
void swap02(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 33;
int b = 99;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "值传递" << endl;
swap02(a, b);
// 不会改变传入的实参的值
cout << "a = " << a << endl; // 33
cout << "b = " << b << endl; // 99
cout << "地址传递" << endl;
swap01(&a, &b);
// 可以看到这里的值已经被改变了
cout << "a = " << a << endl; // 99
cout << "b = " << b << endl; // 33
system("pause");
return 0;
}
// 交换两个数的函数, 通过解引用的方式交换两个数
void swap01(int* a, int* b) { // 把房间号传递过来了,可以借助房间号,改变房间里放的东西
int temp = *a;
*a = *b;
*b = temp;
}
案例描述:实现一个函数,利用冒泡排序对整数数组进行降序排序
#include
using namespace std;
// 输出数组内容
void printArray(int* arr, int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << "\t";
}
cout << endl;
}
// 冒泡排序
void bubbleSort(int* arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = { 9, 34, 56, 7, 123, 45, 67, 98, 33, 12, 55, 987, 11, -2 };
int* arrP = arr; // arr就是数组的首地址
bubbleSort(arr, size(arr));
printArray(arr, size(arr));
system("pause");
return 0;
}
struct 结构体名 { 结构体成员列表 };
struct 结构体名 变量名;
struct 结构体名 变量名 = { 成员值1, 成员值2... };
#include
using namespace std;
// 定义结构体
struct Student {
string name;
int age;
double score;
} s0; // 创建结构体的时候顺便创建结构变量(不推荐)
void printStudent(Student stu) {
cout << "name: " << stu.name << " age: " << stu.age << " score: " << stu.score << endl;
}
int main() {
// 这种方式struct关键字能省略不写
struct Student s1;
s1.name = "叶子";
s1.age = 18;
s1.score = 100;
printStudent(s1);
// 按顺序填入结构体数据
Student s2 = { "老黄", 25, 60 };
printStudent(s2);
// 创建的时候顺便创建的量
s0.name = "YeZi";
s0.age = 25;
s0.score = 100;
printStudent(s0);
system("pause");
return 0;
}
struct 结构体名 数组名[元素个数] = { {} , {}, ... {} }
#include
using namespace std;
struct Student {
// 成员列表
string name; // 姓名
int age; // 年龄
int score; // 分数
};
int main() {
// 结构体数组
Student stus[3] = {
{"叶子", 18, 100},
{"老黄", 20, 99},
{"罗磊", 21, 100}
};
// 遍历结构体数组
for (int i = 0; i < size(stus); i++) {
cout << "姓名:" << stus[i].name
<< " 年龄:" << stus[i].age
<< " 分数:" << stus[i].score << endl;
}
system("pause");
return 0;
}
->
可以通过结构体指针访问结构体的属性#include
using namespace std;
struct Student {
// 成员列表
string name; // 姓名
int age; // 年龄
int score; // 分数
};
int main() {
Student stu0 = { "老黄", 20, 99 };
// 指针指向结构体变量
Student* p = &stu0;
// 通过指针访问结构体变量中的数据
cout << "姓名: " << p->name << " 年龄: " << p->age << " 分数: " << p->score << endl;
system("pause");
return 0;
}
#include
using namespace std;
struct Student {
// 成员列表
string name; // 姓名
int age; // 年龄
int score; // 分数
};
struct Teacher {
string name;
int age;
Student stu;
};
int main() {
Teacher teacher = {"娟", 18, {"叶子", 18, 100}};
cout << "老师姓名: " << teacher.name << " 老师年龄: " << teacher.age
<< " 学生姓名: " << teacher.stu.name << " 学生年龄: " << teacher.stu.age << " 学生分数: " << teacher.stu.score << endl;
system("pause");
return 0;
}
#include
using namespace std;
struct Student {
string name;
int age;
double score;
};
// 输出函数,值传递不影响实参变量
void printStudent(Student stu, string tag) {
cout <<"tag: " << tag << " name: " << stu.name << " age : " << stu.age << " score : " << stu.score << endl;
}
// 值传递,在函数中修改结构体不会影响具体值
void printStudent1(Student stu) {
stu.name = "luilui";
stu.age = 20;
stu.score = 99;
printStudent(stu, "printStudent1");
}
// 引用传递,函数中修改结构体会影响实参的值
void printStudent2(Student* ps) {
ps -> name = "老黄";
ps -> age = 20;
ps -> score = 99;
printStudent(*ps, "printStudent2");
}
int main() {
Student stu;
stu.name = "叶子";
stu.age = 18;
stu.score = 100;
cout << "值传递: " << endl;
printStudent(stu, "main");
printStudent1(stu);
printStudent(stu, "main"); // 函数里修改了具体的值,但是值传递不影响实参结构体
cout << "\n\n引用传递: " << endl;
printStudent(stu, "main");
printStudent2(&stu); // 取地址符,传入的是对应结构体的指针
printStudent(stu, "main"); // 引用传递,函数里边的修改影响实参结构体
system("pause");
return 0;
}
示例:
#include
using namespace std;
struct Student {
string name;
int age;
double score;
};
// 3值传递时,会将实参复制给形参,数据量越大,占用的内存越多
void printStruct(Student stu) {
cout << "name: " << stu.name << " age: " << stu.age << " score: " << stu.score << endl;
}
// 4将函数中的形参改为指针,不会复制,所以会介绍内存空间的占用
void printStruct(const Student *stu) { // 6加入const(常量指针)可以防止在形参修改,影响到实参的值
// 5这样会留下一个隐患,地址传递时对形参指针的修改会影响到实参的值
// stu->name = "老黄"; 7加入const之后会报错,防止误操作
cout << "name: " << stu->name << " age: " << stu->age << " score: " << stu->score << endl;
}
int main() {
//1创建结构体变量
Student s = { "叶子", 18, 100.00 };
//2通过函数打印结构体的信息
printStruct(s);
printStruct(&s);
system("pause");
return 0;
}
#include
using namespace std;
struct Student {
string name;
int score;
};
struct Teacher {
string name;
Student stus[5];
};
void input(Teacher* tchs, int sizeT) {
for (int i = 0; i < sizeT; i++) {
string name = "";
cout << "请输入第" << i << "位老师的姓名:";
cin >> name;
tchs[i].name = name;
cout << "开始录入 " << name << " 老师的学生们!" << endl;
for (int j = 0; j < size(tchs[i].stus); j++) {
string name = "";
cout << "输入学生" << j << "的姓名:";
cin >> name;
tchs[i].stus[j].name = name;
cout << "输入 " << name << " 的成绩:" << endl;
int score = 0;
cin >> score;
tchs[i].stus[j].score = score;
}
}
}
void input1(Teacher tchs[], int sizeT) {
for (int i = 0; i < sizeT; i++) {
string name = "";
cout << "请输入第" << i << "位老师的姓名:";
cin >> name;
tchs[i].name = name;
cout << "开始录入 " << name << " 老师的学生们!" << endl;
for (int j = 0; j < size(tchs[i].stus); j++) {
string name = "";
cout << "输入学生" << j << "的姓名:";
cin >> name;
tchs[i].stus[j].name = name;
cout << "输入 " << name << " 的成绩:" << endl;
int score = 0;
cin >> score;
tchs[i].stus[j].score = score;
}
}
}
void print(Teacher* tchs, int sizeT) {
for (int i = 0; i < sizeT; i++) {
cout << tchs[i].name << " 老师的学生信息:" << endl;
for (int j = 0; j < size(tchs[i].stus); j++) {
string name = "";
cout << tchs[i].stus[j].name << " 的成绩是: " << tchs[i].stus[j].score << endl;
}
}
}
int main() {
Teacher teachers[3];
input(teachers, size(teachers));
print(teachers, size(teachers));
system("pause");
return 0;
}
#include
#include
using namespace std;
/*
设计一个英雄的结构体,包括成员 姓名,年龄,性别
创建结构体数组,数组中存放5名英雄。
通过冒泡排序法将数组中的英雄按照年龄进行升序排序,打印最终结果。
*/
// 英雄结构体
struct Hero {
string name;
int age;
string sex;
};
// 随机获取英雄的年龄
int getRandAge() {
int age = rand() % 51;
if (age <= 21) { // 小于201岁重新获取
return getRandAge();
}
return age;
}
// 初始化英雄数据
void initHeroInfo(Hero heros[], int len) {
srand((unsigned int)time(NULL));
heros[0] = { "刘备", getRandAge(), "男"};
heros[1] = { "关羽", getRandAge(), "男" };
heros[2] = { "张飞", getRandAge(), "男" };
heros[3] = { "赵云", getRandAge(), "男" };
heros[4] = { "貂蝉", getRandAge(), "女" };
heros[5] = { "西施", getRandAge(), "女" };
}
// 冒泡排序
void sortHeros(Hero *heros, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - i - 1; j++) {
if (heros[j].age > heros[j + 1].age) {
Hero temp = heros[j];
heros[j] = heros[j + 1];
heros[j + 1] = temp;
}
}
}
}
// 打印英雄信息
void printHeros(const Hero *heros, int len) {
for (int i = 0; i < len; i++) {
cout << "姓名: " << heros[i].name << " 年龄: " << heros[i].age << " 性别: " << heros[i].sex << endl;
}
cout << endl;
}
int main() {
Hero heros[6];
initHeroInfo(heros, size(heros));
printHeros(heros, size(heros));
sortHeros(heros, size(heros));
printHeros(heros, size(heros));
system("pause");
return 0;
}
学习笔记与课程计划
B站视频链接