C++ 学习笔记 (11)—— 指针
1、指针的基本概念
作用:
可以通过指针间接访问内存
内存编号是从0开始记录的,一般用十六进制数字表示 可以利用指针变量保存地址
2 、指针变量的定义和使用
指针变量定义语法:
数据类型 * 变量名:
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "指针的定义和使用:" << endl;
//1.指针的定义
//指针定义的语法:数据类型 * 指针变量
int a = 89;
//让指针记录变量a的地址
//可以通过解引用方式来找到指针指向的内存
//指针前加 * 号代表解引用,找到指针指向的内存中的数据
int * p = &a;
cout << "a ="<< a << endl;
cout << "a的地址是:"<< &a << endl;
cout << "p =" << p<<endl;
//2.指针的使用
*p = 10; cout << "a = " << a << endl;
cout <<"*p = " << *p << endl;
system("pause");
return 0;
}
3 、指针所占内存空间
32位操作系统 占 4个字节
64位操作系统 占 8个字节
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "指针所占内存空间练习:" << endl;
//指针所占内存空间
int a = 10;
int *p = &a;
cout << "sizeof (int *) :" << sizeof(int *)<<"个字节" << endl;
cout << "sizeof (float *) :" << sizeof(float *) << "个字节" << endl;
cout << "sizeof (double *) :" << sizeof(double *) << "个字节" << endl;
cout << "sizeof (char *) :" << sizeof(char *) << "个字节" << endl;
system("pause");
return 0;
}
4、空指针和野指针
空指针:
指针变量指向内存中编号为0的空间
用途:
初始化指针变量
注意:
空指针指向的内存是不可以访问的
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "空指针练习:" << endl;
//空指针
//1.空指针用于给指针变量进行初始化
int * p = NULL;
//2.空指针是不可以进行访问的
//0~255之间的内存编号是系统占用的
*p = 100; //执行调试 会报错
system("pause");
return 0;
}
野指针
指针变量指向非法的内存空间
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "野指针练习:" << endl;
//野指针
//在程序中尽量避免野指针
int *p = (int *)0x1100;
cout << *p << endl; //执行调试会报错
system("pause");
return 0;
}
总结:空指针和野指针都不是我们申请的内存空间,因此不要进行访问
5、const修饰指针
const修饰指针有三种情况:
1.const修饰指针 --常量指针
2.const修饰变量 --指针常量
3.const即修饰指针,又修饰常量
eg:
#include <iostream>
using namespace std;
int main()
{
cout << "const修饰指针练习:" << endl;
int a = 10;
int b = 90;
//常量指针 const 修饰指针
//可以修改指针指向的地址,不能修改指针所指的内存中的值
const int* p = &a;
//*p = 89; //解引用不能被修改
cout << *p << endl;
p = &b;
cout << *p << endl;
//指针常量 const 修饰变量
//可以进行解引用,修改内存中的值,不可以修改指向的地址
int* const pp = &a;
*pp = 41;
cout << *pp << endl;
//pp = &b; //不能再指向其他的地址
//const即修饰指针,又修饰变量
const int* const ppp = &a;
//ppp = &b; //不可以修改指向的地址
//*ppp = 21; //不可以修改地址中的值
system("pause");
return 0;
}
6、指针和数组
作用:
利用指针访问数组元素
eg;
#include <iostream>
using namespace std;
int main()
{
cout << "指针和数组练习:" << endl;
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr; //数组名就是数组首地址
cout << p << endl;
cout << arr << endl;
//利用指针输出数组元素
//cout << *p << endl;
//p++;
//cout << *p << endl;
//利用for遍历出所有元素
for (int i = 0; i < 9; i++)
{
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
7、指针和函数
作用:
利用指针做函数参数,可以改变实参的值
eg:
#include <iostream>
using namespace std;
//实现两个数字进行交换值
//值传递
void swap01(int a, int b)
{
int temp = 0;
temp = b;
b = a;
a = temp;
}
//地址传递
void swap02(int* a, int* b)
{
int temp = 0;
temp = *b;
*b = *a;
*a = temp;
}
int main()
{
cout << "指针和函数练习:" << endl;
//指针和函数
//1.值传递
int a = 89;
int b = 18;
cout << "交换前:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap01(a, b);
cout << "交换后:" << endl;
cout << "a = " << a<<endl;
cout << "b = " << b << endl;
cout << "******************" << endl;
//2.地址传递
int a1 = 78;
int b1 = 67;
cout << "交换前:" << endl;
cout << "a1 = " << a1 << endl;
cout << "b1 = " << b1 << endl;
swap02(&a1, &b1);
cout << "交换后:" << endl;
cout << "a1 = " << a1 << endl;
cout << "b1 = " << b1 << endl;
system("pause");
return 0;
}
8、案例——指针、数组、函数 (实现冒泡排序)
案例描述: 封装一个函数,利用冒泡排序,实现对整型数组的升序排列
例如数组:int arr[10] = {4,3,6,9,1,2,10,8,7,5}
eg:
#include <iostream>
using namespace std;
//实现冒泡排序——函数
//int* arr 也可以写成 int arr[]
void asorder(int* arr,int len);
//遍历打印数组中的元素
void printArray(int *arr, int len);
int main()
{
cout << "指针数组函数练习:" << endl;
//指针数组函数
//案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排列
//列如数组:int arr[10]={4,3,6,9,1,2,10,8,7,5};
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(arr[0]);
cout << "数组长度 len = " << len << endl;
cout << "排序前:" << endl;
printArray(arr, len);
asorder(arr, len);
cout << "排序后:" << endl;
printArray(arr, len);
system("pause");
return 0;
}
void asorder(int* arr,int len)
{
int temp = 0;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}
void printArray(int *arr, int len)
{
for (int i = 0; i < len;i++)
{
cout << arr[i] << endl;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性