一、指针
- 指针的定义
int a = 10;
// 指针定义的语法;数据类型 * 指针变量
int* p;
// 让指针记录变量a的地址
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p为:" << p << endl;
- 使用指针
// 可以通过解引用的方式找到指针指向的内存
// 指针前加 * 代表解引用,找到指针指向的内存中的数据
*p = 1000;
cout << "a = " << a << endl;
cout << "*p = " << *p << endl;
- 指针占用空间
cout << "sizeof (int *)" << sizeof(int*) << endl;
cout << "sizeof (float *)" << sizeof(float*) << endl;
cout << "sizeof (double *)" << sizeof(double*) << endl;
cout << "sizeof (char *)" << sizeof(char*) << endl;
- 空指针 内存编号为0-255系统占用内存
// 空指针
int * p3 = NULL;
// cout << *p3 << endl; 报错,内存编号为0-255系统占用内存,
// *p3 = 100; 报错,不允许用户访问
- 野指针,指针指向非法的内存空间,没有申请的内存空间是非法内存空间
int * p4 = (int*)0x1100;
// 0x1100这个空间还没有申请就访问,会报错
// cout << *p4 << endl;
- 结构体指针,结构体指针操作属性写法
stu->age
void printStudent(const Student* stu)
{
// stu->age = 100; 操作失败,因为加了const
cout << "姓名:" << stu->name << "年龄:" << stu->age << "分数:" << stu->score << endl;
}
- const修饰地址或常量
int a = 10;
int b = 10;
指针指向的值不能改,指针的指向可以改。eg:
const int* p = &a;
// *p = 20; 错误,指针指向的值不能改
p = &b; // 正确
指针的指向不可以改,指针指向的值可以改。eg:
int* const p2 = &a;
// p2 = &b; // 错误
*p2 = 20;
指针的指向和指针指向的值 都不可以改
const int* const p3 = &a;
//*p3 = 100; 错误
//p3 = &b; 错误
- 指针操作数组
// 4. 指针和数组
int arr[] = { 1,2,3,4,5 };
int* p4 = arr;
cout << "第一个元素:" << arr[0] << endl;
cout << "指针访问的第一个元素 " << *p << endl;
for (int i = 0; i < 5; i++)
{
// 利用指针遍历数组
cout << *p4 << endl;
p4++;
}
int main() {
int* arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = i + 100;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
delete[] arr;
return 0;
}
- new生成的变量,其内存空间不会被释放,直到窗口被关闭,即代码
int main() {
int* p = func_2();
cout << *p << endl; // 10
cout << *p << endl; // 10
cout << *p << endl; // 10
delete p;
// cout << *p << endl; // 错误,delete p, p已经释放了内存
return 0;
}
二、引用
- 数据类型 &别名 = 原名
int a = 10;
int &b = a;
- 引用就是地址值不能修改的地址,必须初始化,初始化后不能更改,所以也是地址传递。
自动转换为 int* const ref = &a;
void func(int& ref)
{
ref = 100;
}
int main()
{
int a = 10;
// 自动转换为 int* const ref = &a; 指针常量指针指向不可改,所以引用不能改
int& ref = a;
ref = 20; // 内部发现ref是引用,自动帮我们转换 *ref = 20;
cout << "a: " << a << endl; // 20
cout << "ref: " << ref << endl; // 20
func(a);
return 0;
}
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void swap02(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swap03(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main() {
int a = 10;
int &b = a; // 数据类型 &别名 = 原名
cout << "a = " << a << endl;
cout << "b = " << b << endl;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// 1.引用必须初始化
// int& b; 错误,必须初始化
// 2.引用在初始化后,不可以改变
// 3. 通过引用参数产生的效果同地址传递是一样的
int c = 10;
int d = 20;
//swap01(c, d);
//swap02(&c, &d);
swap03(c, d);
cout << "c = " << c << endl;
cout << "d = " << d << endl;
return 0;
}
- 局部引用不能作为函数返回值,因为局部引用在函数调用后,其指向的内存数据就会被删除。若被static修饰,不会被删除
int& test01()
{
int a = 10;
return a;
}
int& test02()
{
static int a = 20;
return a;
}
int main()
{
int& ref = test01();
// 1.局部引用不能作为函数返回值,因为局部引用在函数调用后,其指向的内存数据就会被删除
cout << "ref= " << ref << endl; // 10
cout << "ref= " << ref << endl; // 2049755664
int& ref2 = test02();
cout << "ref2= " << ref2 << endl; // 20
cout << "ref2= " << ref2 << endl; // 20
return 0;
}
- 引用(别名)可以做左值
// 2.引用(别名)可以做左值
test02() = 1000;
cout << "ref2= " << ref2 << endl; // 1000
cout << "ref2= " << ref2 << endl; // 1000
- 为防止传到函数中的引用发生变化,有const修饰引用
void showValue(const int& val)
{
// val = 1000; // 加上const
cout << "val = " << val << endl;
}