作用:通过指针间接访问内存
#include
using namespace std;
int main() {
int a = 10;
//1、定义指针
int * p;
//让指针记录变量a的地址
p = &a;
cout << "a address:" << &a << endl;
cout << "p:" << p << endl;
//2、使用指针
//可以通过解引用的方式找到指针指向的内存
//指针前面加*代表解引用,找到指针指向的内存中的数据
*p = 1000;
cout << "a :" << a << endl;
cout << "*p:" << *p << endl;
system("pause");
return 0;
}
#include
using namespace std;
int main() {
//指针所占内存空间大小
int a = 10;
int* p = &a;
//X86 64位存地址都是8个字节
cout << " sizeof(p):" << sizeof(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;
system("pause");
return 0;
}
#include
using namespace std;
int main() {
//空指针
//1、空指针用于初始化
int* p = NULL;
//2、空指针不可以访问
*p = 100;//报错
system("pause");
return 0;
}
#include
using namespace std;
int main()
{
//野指针
//不能随便指定内存地址,地址没有申请,不能访问
//(int *)0x1100:将0x1100转换为地址
int* p = (int *)0x1100;
cout << *p << endl;//报错
system("pause");
return 0;
}
const修饰指针:常量指针(const int * p = &a; 指针的指向可以修改,指针指向的值不可以改,即指针指向的地址可以改,指向地址的数据不能改,10不能改,地址可以改)
* p = 20;错误
p = &b;正确
const修饰常量:指针常量(int * const p = &a; 指针的指向不可改,指针指向的值可以改)
* p = 20;正确
p = &b;错误
const即修饰指针,又修饰常量(const int * const p = &a; 指针的指向和指针的值都不可改)
* p = 20;错误
p = &b;错误
#include
using namespace std;
int main()
{
//const修饰指针
int a = 10;
int b = 10;
const int* p1 = &a;
*p1 = 20;//报错
p1 = &b;
//const修饰常量
int* const p2 = &a;
*p2 = 20;
p2 = &b;//报错
//const即修饰指针,又修饰常量
const int* const p3 = &a;
*p3 = 20;//报错
p3 = &b;//报错
system("pause");
return 0;
}
#include
using namespace std;
int main() {
//指针和数组
//利用指针访问数组中的元素
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "the first element:" << arr[0] << endl;
int* p = arr;//arr是数组首地址
cout << "zhi zhen the first element:" << *p << endl;
p++;//64位往后移4个字节,指针向后偏移4个字节
cout << "zhi zhen the first element:" << *p << endl;
cout << "zhi zhen bian li the element:" << endl;
int* p2 = arr;
for (int i = 0; i < 10; i++)
{
cout << *p2 << endl;
p2++;
}
system("pause");
return 0;
}
#include
using namespace std;
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "swap01 a = " << a << endl;
cout << "swap01 b = " << b << endl;
}
void swap02(int* p1, int* p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
//指针和函数
//1、值传递
int a = 10;
int b = 20;
swap01(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//2、地址传递
swap02(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
案例:封装一个函数,利用冒泡排序,实时对整型数组的升序排列。
#include
using namespace std;
//创建一个函数实现冒泡排序
void bubbleSort(int *arr, int len){
//外层循环次数
for(int i=0; i<len-1; 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;
}
}
}
}
int main(){
//1. 创建一个数组
int arr[10] = {4, 3, 6, 9, 1, 2, 10, 8, 7, 5};
int len = sizeof(arr)/sizeof(arr[0]);
//2. 创建一个函数实现冒泡排序
bubbleSort(arr, len);
//3. 打印排序好的数组
for (int i = 0 ; i < len; i++){
cout<<arr[i]<<endl;
}
system("pause");
return 0;
}