指 针 指针 指针
- 55.指针-指针的定义和使用
- 56.指针-指针所占内存空间
- 57.指针-空指针
- 58.指针-野指针
- 59.指针-const修饰指针
- 60.指针-指针和数组
- 61.指针-指针和函数
- 62.指针-指针配合数组和函数案例
指针的基本概念
指针的作用:可以通过指针间接访问内存
- 内存编号是从0开始记录的,一般用十六进制数字表示。
- 可利用指针变量保存地址
55.指针-指针的定义和使用
指针变量定义语法:数据类型*变量名;
#include<iostream>
using namespace std;
int main() {
//1. 定义指针
int a = 10;
//指针定义的语法:数据类型 * 指针变量名
int * p;
//让指针记录变量a的地址
p = &a;
cout << "a的地址: " << &a << endl;
cout << "指针p为:" << p << endl;
//2. 使用指针
// 可以通过解引用的方式来找到指针指向的内存
// 指针前加 * 代表解引用,找到指针指向的内存中的数据
*p = 1000;
cout << "a= " << a << endl;
cout << "*p= " << *p << endl;
}
56.指针-指针所占内存空间
在32位操作系统下,占用4个字节
在64位操作系统下,占用8个字节
#include<iostream>
using namespace std;
int main() {
// 指针所占内存空间
int a = 10;
int * p = &a;
cout << "size of (int *)" << sizeof(p) << endl;
system("pause");
}
#include<iostream>
using namespace std;
int main() {
// 指针所占内存空间
int a = 10;
int * p = &a;
cout << "size of (int *)" << sizeof(p) << endl;
cout << "size of (int *)" << sizeof(float *) << endl;
cout << "size of (int *)" << sizeof(double *) << endl;
cout << "size of (int *)" << sizeof(char *) << endl;
system("pause");
}
57.指针-空指针
空指针:指针变量指向内存中编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存是不可以访问的
#include<iostream>
using namespace std;
int main() {
// 空指针
// 1.空指针用于给指针进行初始化
int * p = NULL;
// 2.空指针是不可以进行访问的
// 0-255 之间的内存编号是系统占用的,不可以访问
*p = 100;
system("pause");
return 0;
}
58.指针-野指针
野指针:指针变量指向非法的内存空间
#include<iostream>
using namespace std;
int main() {
// 野指针
int * p = (int *)0x1100;
cout << *p << endl;
system("pause");
return 0;
}
59.指针-const修饰指针
const修饰指针有三种情况:
- 1.const修饰指针—常量指针
- 2.const修饰常量—指针常量
- 3.const即修饰指针,又修饰常量
1.常量指针:指针的指向可以修改,但是指针指向的值不可以改
常量指针,重要的是量,所以量不可以改
int a = 100;
int b = 300;
const int * p = &a;
*p = 200; //错误
p = &b; // 可以
#include<iostream>
using namespace std;
int main() {
int a = 100;
int b = 300;
const int * p = &a;
//* p = 200; //错误
p = &b; // 可以
cout << *p << endl;
system("pause");
return 0;
}
2. 指针常量:指针的指向不可以改,指针指向的值可以改
指针常量,重要的是针,所以指向不可以改
int a = 100;
int b = 300;
int * const p = &a;
*p = 200; //可以
p = &b; // 错误
#include<iostream>
using namespace std;
int main() {
int a = 100;
int b = 300;
int * const p = &a;
//* p = 200; //错误
* p = 5000; // 可以
cout << * p << endl;
system("pause");
return 0;
}
3.const即修饰指针,又修饰常量:指针的指向和指针指向的值都不可以改
int a = 100;
int b = 300;
int * const p = &a;
*p = 200; //错误
p = &b; // 错误
#include<iostream>
using namespace std;
int main() {
int a = 100;
int b = 300;
const int * const p = &a;
//p = &b; //错误
//* p = 5000; // 错误
cout << * p << endl;
system("pause");
return 0;
}
60.指针-指针和数组
作用:利用指针访问数组中元素
#include<iostream>
using namespace std;
int main() {
// 指针和数组
// 利用指针访问数组中的元素
int arr[5] = { 11,22,33,44,55 };
cout << "第一个元素:" << arr[0] << endl;
int *p = arr; // arr就是数组首地址
cout << "利用指针访问第一个元素:" << * p << endl;
p++; // 指针向后偏移四个字节
cout << "利用指针访问第二个元素:" << * p << endl;
p = p + 2;
cout << "利用指针访问第四个元素:" << *p << endl;
system("pause");
return 0;
}
61.指针-指针和函数
作用:利用指针作函数参数,可以修改实参的值
问题:形参改变,实参未变
#include<iostream>
using namespace std;
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "s_a = " << a << endl;
cout << "s_b = " << b << endl;
}
void swap2(int * p1,int * p2) {
int temp = * p1;
* p1 = * p2;
* p2 = temp;
}
int main() {
// 指针和函数
int a = 100;
int b = 200;
// 1. 值传递
//swap(a, b);
// 2. 地址传递
swap2(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
62.指针-指针配合数组和函数案例
案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序
#include<iostream>
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++) {
// 如果j>j+1的值,交换数字
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 << arr[i] << endl;
}
}
int main() {
// 1.创建数组
int arr[10] = { 6,3,5,10,1,9,15,18,20,30 };
// 数组长度
int len = sizeof(arr) / sizeof(arr[0]);
// 2.创建函数,实现冒泡排序
bubbleSort(arr, len);
// 3.打印排序后的数组
printArray(arr,len);
system("pause");
return 0;
}
如何获取变量类型 并输出
#include<iostream>
using namespace std;
int main() {
// 创建数组
int arr[10] = { 6,3,5,10,1,9,15,18,20,30 };
int * p = NULL;
p = arr;
// 如何获取变量类型 并输出
cout << typeid(arr).name() << endl;
cout << p[2] << endl;
cout << typeid(p).name() << endl;
system("pause");
return 0;
}