指针的定义和使用:
1、指针:是一种数据类型 指针变量也是一种变量
2、指针格式: 对应的数据类型 * p:指针类型变量 用来指向一个变量的地址
3、通过指针修改变量的值
#include "iostream"
#include "string"
using namespace std;
int main()
{
int a = 10;
a = 20;//修改变量的值
int b = 100;
//指针是一种数据类型p是指针类型的变量 用来指向一个变量的地址
int* p = &a;//p指向a,p是a的地址
p = &b; //p被重新赋值,p与a的联系断开,p的值就是b的地址
//下面的2个输出是一样的,都是地址
cout << "&b====" << &b << endl; //&b====012FFB78
cout << "p====" << p << endl; //p====012FFB78
//通过指针修改变量的值,p是b的地址,*p是b的内容
*p = 200;
//下面的2个输出都是实际的内容都是200,已经通过指针修改了地址中的内容
cout << "b====" << b << endl; //b====200
cout << "*p====" << *p << endl; //*p====200
//sizeof()指针类型在内存中的大小p是int*类型的
//在32位操作系统中所有的指针的大小是4个字节
cout << "sizeof(p)====" << sizeof(p) << endl; //sizeof(p)====4
cout << "sizeof(int* )====" << sizeof(int*) << endl; //sizeof(int* )====4
system("pause");
return EXIT_SUCCESS;
}
野指针、空指针与 万能指针
1、野指针:野指针是指向一个未知的内存空间,可能在读写的时候出现错误。
0-255都是系统保留的 不可以读,不可以写
int main()
{
int a = 10;
int* p = &a;
//指向内存编号位0xff00的内存地址,是一个未知的空间
//p是野指针,野指针是指向一个未知的内存空间,可能在读写的时候出现错误
p = 0xff00;
system("pause");
return EXIT_SUCCESS;
}
2、空指针 没有指向任何的地址(其指向0的地址)
空制指针就是指向内存编号为零的空间,操作该内存空间会报错,一般情况空指针用于程序条件判断
int main()
{
int* p;
//空指针是指向内存编号为0的空间 操作该内存空间会报错,一般空指针用作条件判断
p = NULL;//p是空指针,没有指向任何内容
*p = 200;
if (P ! = NULL)
{
free();
}
system("pause");
return EXIT_SUCCESS;
}
3、万能指针:void * 指针可以指向任意变量的内存空间
int main()
{
int a = 10;
void* p = &a;//
//使用的时候需要改变成对应的类型,首先将p强转成int*类型的指针,再通过*获取值
//(int*)p是修改完成指针为整型指针,
*(int*)p = 200;
cout << "a=====" << a << endl;//输出是200
cout << "*(int*)p====" << *(int*)p << endl;//输出是200
system("pause");
return EXIT_SUCCESS;
}
万能指针的使用
int main()
{
int arr[10] = {0};
void* p = arr;//p指向arr的第一个数据arr[0]
*(int*)p = 200; //arr[0] = 200
*((int*)p + 1) = 13; //(int*)先强转万能指针p的类型,再修改arr[1]的内容
*((int*)p + 4) = 400;//(int*)先强转万能指针p的类型,再修改arr[4]的内容
for (int i = 0; i < 10; i++)
{
cout << "第" << i << "个数据是===" << arr[i] << endl;
}
system("pause");
return EXIT_SUCCESS;
}
多级指针
#include "iostream"
using namespace std;
int main()
{
int a = 10;
int* p = &a;//p是一级指针,存放a的地址
int** pp = &p;//q是二级指针,存放的是p的地址
//*pp表示一级指针的值,也就是p,&a
//**pp表示一级指针地址指向的值,也就是*p,a
cout << "" << << endl;
system("pause");
return EXIT_SUCCESS;
}
指针作为返回值
下面的p的输出是乱码
#include "iostream"
#include "string"
using namespace std;
char* test()
{
char arr[] = "hello world";
return arr;
}
int main()
{
char* p = test();
cout << "*p====" << *p << endl; //*p====h
cout << "p====" << p << endl; //p====龋
system("pause");
return EXIT_SUCCESS;
}
字符串常量
#include "iostream"
#include "string"
using namespace std;
char* test()
{
//字符数组 创建位置再栈区,下面的return执行结束以后,相关的空间会销毁
char arr[] = "hello world";
//字符串常量 会在程序运行时运行 放在常量区,内容不能被修改,但是可以被读取。
//程序结束的时候被销毁
char* arr = "hello world";
return arr;
}
int main()
{
char* p = test();
cout << "*p====" << *p << endl; //*p====h
cout << "p====" << p << endl; //p====hello world
system("pause");
return EXIT_SUCCESS;
}
字符串排序
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
//字符串排序 根据字符串首字符 按照a-z的顺序排序
//student tree new bee bee new student tree
void bubble(char ** arr, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
//比对两个字符串的首字母
//1、指针判断
if (**(arr + j) < **(arr + j + 1))
{
char * temp = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = temp;
}
//2、数组判断
if (arr[j][0] > arr[j + 1][0])
{
char * temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
//3、混合判断
if (*arr[j] > *arr[j + 1])
{
char * temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
char *arr[] = { "cshdf", "ehsdhf", "bjhdjfhd", "abee" };
/*arr[0][0]
student //arr[0]
tree//arr[1]
new
bee
*/
bubble(arr, 4);
for (int i = 0; i < 4; i++)
{
cout << "arr[" << i << "]=======" << arr[i] << endl;
}
cout << "arr[0][0]=======" << arr[0][0] << endl;
cout << "arr[1][0]=======" << arr[1][0] << endl;
cout << "arr[2][0]=======" << arr[2][0] << endl;
cout << "arr[3][0]=======" << arr[3][0] << endl;
system("pause");
return EXIT_SUCCESS;
}
本文深入探讨C++中指针的基本概念,包括定义、使用、野指针、空指针与万能指针等,同时讲解了多级指针、指针作为返回值的应用,并通过实例演示了字符串排序的过程。

被折叠的 条评论
为什么被折叠?



