(一)指针
1.空指针 int * p = NULL;
指向内存中编号为0的空间,用来初始化指针变量, 其指向的内存是不可访问的(因为0~255间的内存编号由系统占用)
注:
指针前加*表示解引用,用来找到该指针指向的内存中的数据。所以当形参是 &引用或指针的指针** 时,改变形参就是改变实参。
2.野指针
指向非法的内存空间。野指针和空指针都不是我们申请的空间,故无法访问。
3.const修饰指针有3种情况(根据const与*的前后关系,就能辨别不同的叫法)
(1)const修饰指针——常量指针
指针的指向可改,指向的值不可改。
(2)const修饰常量——指针常量
指针的指向不可改,指向的值可改。
(3)const既修饰指针,又修饰常量
指针的指向与指向的值都不可改。
例:
4. 值传递只改变形参,不影响实参。地址传递都影响。
例1
例2
(二)结构体
1.定义
2.结构体数组
3.结构体指针
通过结构体指针访问结构体里的属性,需要利用‘—>’
4.结构体嵌套结构体
5.结构体做函数参数
6. 结构体中的const使用场景
把函数中的形参改为指针,可以大幅减少内存空间的占用,不会复制新的副本出来
此时加const得到常量指针,解决地址传递带来的参数误更改问题:
举例1
举例2
7. 项目实战 —— 通讯录管理系统
#include <iostream>
using namespace std;
#include <string>
#define MAX 1000
//设计联系人结构体
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//设计通讯录结构体
struct Addressbooks
{
struct Person personArray[MAX];//通讯录保留的联系人数组
int m_Size;//通讯录当前记录的联系人个数
};
//1、添加联系人
void addPerson(Addressbooks* abs)
{
if(abs->m_Size == MAX)
{
cout << "通讯录已满,无法添加!\n" ;
return;
}
else
{
//姓名
string name;
cout << "请输入姓名: \n";
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
//性别
cout << "请输入性别: \n";
cout << "1 --- 男 \n";
cout << "2 --- 女 \n";
int sex = 0;
while (1)
{
cin >> sex;
if(sex==1 || sex==2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
cout << "输入有误,请重新输入! \n";
}
//年龄
cout << "请输入年龄: \n";
int age = 0;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
//电话
cout << "请输入电话: \n";
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
//住址
cout << "请输入住址: \n";
string address;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
//更新通讯录的当前人数
abs->m_Size++;
cout << "添加成功!\n";
// system("pause");//请按任意键继续
// system("cls");//清屏
}
}
//2、显示所有联系人
void showPerson(Addressbooks* abs)
{
if(abs->m_Size == 0)
cout << "当前记录为空 \n";
else
for(int i = 0; i < abs->m_Size; i++)
{
cout << "姓名" << abs->personArray[i].m_Name << "\t";
cout << "性别" << abs->personArray[i].m_Sex << "\t";
cout << "年龄" << abs->personArray[i].m_Age << "\t";
cout << "电话" << abs->personArray[i].m_Phone << "\t";
cout << "住址" << abs->personArray[i].m_Addr << endl;
}
// system("pause");
// system("cls");
}
//判断指定联系人是否存在
int isExist(Addressbooks* abs, string name)
{
for(int i = 0; i < abs->m_Size; i++)
if(abs->personArray[i].m_Name == name)
return i;//找到
return -1;//没找到
}
//3、删除指定联系人
void deletePerson(Addressbooks* abs)
{
cout << "请输入删除联系人姓名: \n";
string name;
cin >> name;
int ret = isExist(abs,name);
if(ret != -1)
{
//找到此人,做删减
for(int i = ret; i < abs->m_Size; i++)
abs->personArray[i] = abs->personArray[i+1];//数据依次前移
abs->m_Size--;//实时更新通讯录上的人数
cout << "删除成功!\n";
}
else
cout << "查无此人\n";
// system("pause");
// system("cls");
}
//4、查找指定联系人
void findPerson(Addressbooks* abs)
{
cout << "请输入您要查找的联系人: \n";
string name;
cin >> name;
int ret = isExist(abs,name);
if(ret != -1)
{
cout << "姓名: " << abs->personArray[ret].m_Name << "\t";
cout << "性别: " << abs->personArray[ret].m_Sex << "\t";
cout << "年龄: " << abs->personArray[ret].m_Age << "\t";
cout << "电话: " << abs->personArray[ret].m_Phone << "\t";
cout << "住址: " << abs->personArray[ret].m_Addr << endl;
}
else
cout << "查无此人\n";
// system("pause");
// system("cls");
}
//5、修改指定联系人
void modifyPerson(Addressbooks* abs)
{
cout << "请输入您要修改的联系人: \n";
string name;
cin >> name;
int ret = isExist(abs,name);
if(ret != -1)
{
string name;
cout << "请输入姓名:\n";
cin >> name;
abs->personArray[ret].m_Name = name;
cout << "请输入性别:\n";
cout << "1 --- 男\n";
cout << "2 --- 女\n";
int sex = 0;
while (1)
{
cin >> sex;
if(sex == 1 || sex == 2)
{
abs->personArray[ret].m_Sex = sex;
break;
}
cout << "输入有误,请重新输入!\n";
}
int age = 0;
cout << "请输入年龄:\n";
cin >> age;
abs->personArray[ret].m_Age = age;
string phone;
cout << "请输入电话:\n";
cin >> phone;
abs->personArray[ret].m_Phone = phone;
string address;
cout << "请输入地址:\n";
cin >> address;
abs->personArray[ret].m_Addr = address;
cout << "修改成功!\n";
}
else
cout << "查无此人\n";
// system("pause");
// system("cls");
}
//6、清空联系人
void cleanPerson(Addressbooks* abs)
{
abs->m_Size = 0;
cout << "通讯录已清空 \n";
// system("pause");
// system("cls");
}
void ShowMenu()
{
cout << "***************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***************************" << endl;
}
int main()
{
Addressbooks abs;
abs.m_Size = 0;
int select = 0;
while (1)
{
ShowMenu();
cin >> select;
switch (select)
{
case 1://添加
addPerson(&abs);//地址传递,修改实参
break;
case 2://显示
showPerson(&abs);
break;
case 3://删除
deletePerson(&abs);
break;
case 4://查找
findPerson(&abs);
break;
case 5://修改
modifyPerson(&abs);
break;
case 6://清空
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
default:
break;
}
}
}