当前位置:网站首页>7. 指针
7. 指针
2022-06-21 12:06:00 【我想要去航海】
7.1 指针的基本概念
指针的作用:可以通过指针间接访问内存。
内存编号是从0开始记录的,一般用十六进制数字表示。
可以利用指针变量保存地址。
7.2 指针变量的定义和使用
指针变量定义语法: 数据类型* 变量名
7.3 指针所占的内存空间
指针所占的内存是4个字节。
7.4 空指针和野指针
空指针:指针变量指向内存中编号为0的空间。
用途:初始化指针变量。
注意:空指针指向的内存是不可以访问的。
野指针:指针变量指向非法的内存空间。
7.5 const修饰指针
const修饰指针有三种情况
1. const修饰指针——常量指针
const int* p = &a;
特点:指针的指向可以修改,但是指针指向的值不可以改。
2. const修饰常量——指针常量
int* const p = &a;
特点:指针的指向不可以改,指针指向的值可以改
3. const既修饰指针,又修饰常量
const int* const p = &a;
特点:指针的指向和指针指向的值都不可以改。
7.6 指针和数组
作用:利用指针访问数组中的元素。
7.7 指针和函数
作用:利用指针作函数参数,可以修改实参的值。
7.8 指针、数组、函数
案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序。
#include<iostream>
using namespace std;
void bubbleSort(int* arr, int len);
void printArray(int* arr, int len);
int main()
{
int arr[10]={
4, 3, 6, 9, 1, 2, 10, 8, 7, 5};
int len=sizeof(arr)/sizeof(arr[0]);
cout<<"排序前:"<<endl;
printArray(arr, len);
bubbleSort(arr, len);
cout<<"排序后:"<<endl;
printArray(arr, len);
system("pause");
return 0;
}
void bubbleSort(int* arr, int len)
{
for(int i=0; i<len; 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 printArray(int* arr, int len)
{
for(int i=0; i<len; i++)
{
cout<<"\t数组第"<<i+1<<"个元素是:\t"<<arr[i]<<endl;
}
}
边栏推荐
猜你喜欢
随机推荐
第八章 Web项目测试
【云原生 | Devops篇】Jenkins安装与实战(二)
Redis最大内存淘汰策略
i.MX - RT1052输入输出(GPIO)
Inheritance and override of methods
Ansible operating instructions for configuring SSH authentication free for the first time
i. MX - rt1052 pulse width modulation (PWM)
华为云发布桌面IDE-CodeArts
STM32开发之 VS Code + GDB下载调试
Redis-bitmap 位图
20n10-asemi medium and low voltage MOS tube 20n10
"Forget to learn again" shell process control - 36. Introduction to the for loop
事务Transaction
Creation mode - singleton mode
Introduction to CPU, MPU, MCU, SOC and MCM
LeetCode-高度检查器
Understand UML class diagram and sequence diagram
Travel does not heal the soul
SDCC编译器 + VSCode开发 8位微控制器
Vs code + GCC environment compilation for STM32 development






![[untitled]](/img/97/7a3dac6e07318613090722ec620e32.png)

