指针
1 指针的基本概念
作用:可以通过指针简介访问内存
- 内存编号是从0开始记录的,一般用用16进制数字表示
- 可以利用指针变量保存地址
2 指针的变量定义和使用
指针变量定义语法:数据类型 *变量名; 代码示例: 1、指针就是地址; 2、指针可以访问内存地址所对应的值,也可以修改该值
int main()
{
int a = 10;
int* p;
p = &a;
cout << "a的地址是:" << &a<<endl;
cout << "指针p=" << p << endl;
cout<<"=======================" << endl;
*p = 1000;
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
return 0;
}
结果:
3 指针所占内存空间
提问:指针也是一种数据类型,那么,它占了多少内存空间?
int main()
{
int a = 10;
int* p = &a;
cout << "sizeof int * =" <<sizeof(p) <<endl;
cout << "sizeof float * =" << sizeof(float*) << endl;
cout << "sizeof double * =" << sizeof(double*) << endl;
cout << "sizeof char * =" << sizeof(char*) << endl;
return 0;
}
结果: 64为操作系统: 32位操作系统: 结论:不管是什么数据类型,在32为操作系统下,指针都是占4个字节大小,在64位操作系统下,指针都是占8个字节大小。
4 空指针和野指针
空指针:指针变量指向内存中编号为0的空间 用途:初始化指针变量 注意:空指针指向的内存是不可以访问的
代码示例: 空指针是不可以进行访问的
int* p = NULL;
*p = 100;
假设定义了一个空指针,然后解引用,将空指针所在地址的值变成100,则会报警告: 运行时则会报错:
野指针:指针变量指向非法的内存空间 代码示例:
int* p = (int *)0x1100;
cout << *p << endl;
结果: 总结:空指针和野指针都不是我们申请的空间,因此不要访问
5 const修饰指针
const修饰有3种情况:
- const修饰指针 —常量指针
- const修饰常量 —指针常量
- const既修饰指针,又修饰常量
1、常量指针 常量指针的指向可以改,但指针指向的值不可以改。
代码示例:
int a = 10;
int b = 10;
const int* p = &a;
p = &b;
int* const p2 = &a;
*p2 = 100;
const int* const p3 = &a;
6 指针和数组
作用:利用指针访问数字中的元素 代码示例:
int arr[] = {1,2,3,4,5,6,7,8,9,10};
cout << "第一个元素为:"<<arr[0] << endl;
int* p = arr;
cout << "指针访问的第一个元素为:" << *p << endl;
p++;
cout << "指针访问的第二个元素为:" << *p << endl;
cout << "for循环指针遍历数组:" << endl;
int* p2 = arr;
for (int i = 0; i < 10; i++)
{
cout << *p2 << " ";
p2++;
}
cout << endl;
结果:
7 指针和函数
作用:利用指针作函数参数,可以修改实参的值 ????????之前有一节我们说过,形参不会改变实参的值,但是,在指针中,形参可以改变实参的值。 代码示例:
void swap001(int a,int b)
{
int temp = a;
a = b;
b = temp;
}
void swap002(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void value_swap()
{
int a = 10;
int b = 20;
swap001(a, b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "============="<<endl;
swap002(&a, &b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
}
结果:
8 指针、数组和函数的小案例
描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序 例如数组 int arr[10]={4,3,6,9,1,2,10,8,7,5}; 代码示例:
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;
}
}
}
}
void print_arr(int* arr, int len)
{
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void case_point_array_func() {
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(arr[0]);
bubblesort(arr, len);
print_arr(arr, len);
}
结果: 本章所有代码:
#include<iostream>
#include<string>
using namespace std;
void point_define()
{
int a = 10;
int* p;
p = &a;
cout << "a的地址是:" << &a << endl;
cout << "指针p=" << p << endl;
cout << "=======================" << endl;
*p = 1000;
cout << "a=" << a << endl;
cout << "*p=" << *p << endl;
}
void point_space()
{
int a = 10;
int* p = &a;
cout << "sizeof int * =" <<sizeof(p) <<endl;
cout << "sizeof float * =" << sizeof(float*) << endl;
cout << "sizeof double * =" << sizeof(double*) << endl;
cout << "sizeof char * =" << sizeof(char*) << endl;
}
void null_point()
{
int* p = NULL;
*p = 100;
}
void wild_point()
{
int* p = (int *)0x1100;
cout << *p << endl;
}
void const_point()
{
int a = 10;
int b = 10;
const int* p = &a;
p = &b;
int* const p2 = &a;
*p2 = 100;
const int* const p3 = &a;
}
void point_array() {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
cout << "第一个元素为:"<<arr[0] << endl;
int* p = arr;
cout << "指针访问的第一个元素为:" << *p << endl;
p++;
cout << "指针访问的第二个元素为:" << *p << endl;
cout << "for循环指针遍历数组:" << endl;
int* p2 = arr;
for (int i = 0; i < 10; i++)
{
cout << *p2 << " ";
p2++;
}
cout << endl;
}
void swap001(int a,int b)
{
int temp = a;
a = b;
b = temp;
}
void swap002(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void value_swap()
{
int a = 10;
int b = 20;
swap001(a, b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
cout << "============="<<endl;
swap002(&a, &b);
cout << "a= " << a << endl;
cout << "b= " << b << endl;
}
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;
}
}
}
}
void print_arr(int* arr, int len)
{
for (int i = 0; i < 10; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void case_point_array_func() {
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(arr[0]);
bubblesort(arr, len);
print_arr(arr, len);
}
int main()
{
case_point_array_func();
return 0;
}
|