目录
2.2.3--指针变量不再使用时,及时置NULL,指针使用之前检查有效性
前言:继上篇文章后这篇文章将继续为大家分享指针的相关知识,本篇主要是const修饰指针,野指针,assert断言,指针的使用和传址调用等知识点。
往期回顾:
一.const修饰指针
1.1--const修饰变量
--变量是可以修改的,如果把变量的地址交给一个指针变量,通过指针变量可以修改这个变量。但是我们如果想要给一个变量加上一些限制,那就用const修饰,这样就不能修改了。
#include<stdio.h>
int main()
{
const int n = 0;
n = 20;//err,n被const修饰后不能被修改
return 0;
}
在C语言中这里的n本质上还是变量,只是在语法层面上限制了n的修改,n是常变量
但是我们如果绕过n,使用n的地址,虽然打破了语法规则,不过就可以修改n了 ,如下:
#include<stdio.h>
int main()
{
const int n = 0;
int* p = &n;
*p = 20;
return 0;
}
这样是不合理的,那该如何限制这种方法呢,我们可以使用const来修饰指针变量做到这一点。
1.2--const修饰指针变量
--const修饰指针变量,可以放在*的右边和左边,意义不同。
- int * p;//没有const修饰?
- int const * p;//const 放在*的左边做修饰
- int * const p;//const 放在*的右边做修饰
我们直接通过代码来具体分析一下
#include <stdio.h>
//代码1 - 测试⽆const修饰的情况
void test1()
{
int n = 10;
int m = 20;
int* p = &n;
*p = 20;//ok?-ok
p = &m; //ok?-ok
}
//代码2 - 测试const放在*的左边情况
void test2()
{
int n = 10;
int m = 20;
const int* p = &n;
*p = 20;//ok?-no
p = &m; //ok?-ok
}
//代码3 - 测试const放在*的右边情况
void test3()
{
int n = 10;
int m = 20;
int* const p = &n;
*p = 20; //ok?-ok
p = &m; //ok?-no
}
//代码4 - 测试*的左右两边都有const
void test4()
{
int n = 10;
int m = 20;
int const* const p = &n;
*p = 20; //ok?-no
p = &m; //ok?-no
}
int main()
{
//测试⽆const修饰的情况
test1();
//测试const放在*的左边情况
test2();
//测试const放在*的右边情况
test3();
//测试*的左右两边都有const
test4();
return 0;
}
结论:const修饰指针变量时
- const如果放在*的左边,修饰的是指针指向的内容,保证指针指向的内容不能通过指针来改变, 但是指针变量本⾝的内容可变。
-
const如果放在*的右边,修饰的是指针变量本⾝,保证了指针变量的内容不能修改,但是指针指向的内容,可以通过指针改变。
二.野指针
--野指针就是指针指向的位置是不可知的(随机的,不正确的,没有明确限制的),直白的来说就是指针指向的空间是不属于当前程序的。
2.1--野指针成因
--这里将为大家介绍几个常见的野指针形成的原因
2.1.1--指针未初始化
#include <stdio.h>
int main()
{
int* p;//局部变量指针未初始化,默认为随机值
*p = 20;
return 0;
}
2.1.2--指针越界访问
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = &arr[0];
int i = 0;
for (i = 0; i <= 11; i++)
{
//当指针指向的范围超出数组arr的范围时,p就是野指针
*(p++) = i;
}
return 0;
}
2.1.3-- 指针指向的空间释放
#include <stdio.h>
int* test()
{
int n = 100;
return &n;
}
int main()
{
int* p = test();
printf("%d\n", *p);
return 0;
}
2.2--如何规避野指针
2.2.1--指针初始化
#include <stdio.h>
int main()
{
int num = 10;
int* p1 = #
int* p2 = NULL;
return 0;
}
2.2.2--小心指针越界
2.2.3--指针变量不再使用时,及时置NULL,指针使用之前检查有效性
理解方法:我们可以把野指针想象成野狗,野狗放任不管是⾮常危险的,所以我们可以找⼀棵树把野狗拴起来, 就相对安全了,给指针变量及时赋值为NULL,其实就类似把野狗栓起来,就是把野指针暂时管理起来。不过野狗即使拴起来我们也要绕着⾛,不能去挑逗野狗,有点危险;对于指针也是,在使⽤之前,我们也要判断是否为NULL,看看是不是被拴起来起来的野狗,如果是不能直接使⽤,如果不是我们再去使⽤。
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = &arr[0];
int i = 0;
for (i = 0; i < 5; i++)
{
*p = 5;
p++;
}
p = NULL;
//下次使⽤的时候,判断p不为NULL的时候再使⽤
//...
p = &arr[0];//重新让p获得地址
if (p == NULL) //判断
{
printf("p是空指针\n");
}
else
{
//……
}
return 0;
}
2.2.4--避免返回局部变量的地址
如造成野指针的第3个例子,不要返回局部变量的地址。
三.assert断言
#include<assert.h>
assert(p != NULL);
assert ()的优点:
能⾃动标识⽂件和出问题的行号- ⽆需更改代码就能开启或关闭 assert() 的机制--如果已经确认程序没有问题,不需要再做断⾔,就在 #include <assert.h> 语句的前⾯,定义⼀个宏 NDEBUG 。
assert ()的缺点:
- 因为引入了额外检查,增加了程序的运行时间
#define NDEBUG
#include <assert.h>
有了#define NDEBUG这个声明后就会禁用掉程序中所有的assert断言了,如果后续又需要使用的话,直接把这条语句注释了就可以了。
一般我们都会在debug版本中使用,在release版本中会直接优化掉的,assert不会影响用户使用程序的效率
四.指针的使用和传址调用
4.1--strlen的模拟实现优化
--之前我们就用指针实现了strlen函数的作用,经过本章学习后,我们对此进行一个优化
int my_strlen(const char* str)//优化点1
{
int count = 0;
assert(str!=NULL);//优化点2
while (*str)
{
count++;
str++;
}
return count;
}
int main()
{
int len = my_strlen("abcdef");
printf("%d\n", len);
return 0;
}
4.2--传值调用和传址调用
--那么使用指针可以解决那些问题呢?
我们来看看一个例题--写一个函数,交换两个整型变量的值:
先来看看不用指针写出来的对不对吧~
#include <stdio.h>
void Swap1(int x, int y)
{
int tmp = x;
x = y;
y = tmp;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
printf("交换前:a=%d b=%d\n", a, b);
Swap1(a, b);
printf("交换后:a=%d b=%d\n", a, b);
return 0;
}
当我们运行上述代码后会发现并没有产生交换效果,这是为什么呢?大家可以先自己调试找找问题
--因为形参x和y与实参a和b的地址不一样,所以操作x和y不会影响a和b,故swap1函数内就算交换了,回到main函数后也不会对a和b产生影响。swap1在调用的时候,是把变量本身直接传递给了函数,这种调用函数的方法我们在之前就知道了,这种叫传值调用。
结论:实参传递给形参的时候,形参会单独创建⼀份临时空间来接收实参,对形参的修改不影响实参。
所以Swap1是失败的。
那我们该咋办呢?--我们可以使用传址调用(调用函数时,将变量的地址传递给函数),在main函数中直接将a和b的地址传递给swap函数,swap里面就可以直接通过地址操作main函数中的a和b,达到交换效果。
#include <stdio.h>
void Swap2(int* px, int* py)
{
int tmp = 0;
tmp = *px;
*px = *py;
*py = tmp;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
printf("交换前:a=%d b=%d\n", a, b);
Swap2(&a, &b);
printf("交换后:a=%d b=%d\n", a, b);
return 0;
}
结语:本篇文章就到此结束了,指针的学习内容还有很多,会在后续的篇章中继续给大家分享,感谢大家的关注和支持。