深入理解指针(二)
1. const修饰指针
2. 野指针
3. assert断言3
4. 指针的使用和传址调用
1. const修饰指针
1.1 const修饰变量
变量是可以修改的,如果把变量的地址交给一个指针变量,通过指针变量的也可以修改这个变量。但是如果我们希望一个变量加上一些限制,不能被修改,那就要使用const来修饰!
#include <stdio.h>
int main()
{int m = 0;m = 20; //m是可以被修改的const int n = 0;n = 20; //n是不能被修改的return 0;
}
#include <stdio.h>
int main() {const int n = 0;printf("n = %d\n", n);int* p = &n;*p = 20;printf("n = %d\n", n);return 0;
}
运行结果:

1.2 const修饰指针变量
⼀般来讲const修饰指针变量,可以放在*的左边,也可以放在*的右边,意义是不⼀样的。
int * p;//没有const修饰?
int const * p;//const 放在*的左边做修饰
int * const p;//const 放在*的右边做修饰
#include <stdio.h>
void test1() {int n = 10;int m = 20;int* p = &n;*p = 20;p = &m;
}
void test2() {int n = 10;int m = 20;const int* p = &n;*p = 20;p = &m;
}
void test3() {int n = 10;int m = 20;int* const p = &n;*p = 20;p = &m;
}
void test4() {int n = 10;int m = 20;int const * const p = &n;*p = 20;p = &m;
}
int main() {//测试⽆const修饰的情况test1();//测试const放在*的左边情况test2();//测试const放在*的右边情况test3();//测试*的左右两边都有consttest4();return 0;
}
2. 野指针
概念: 野指针就是指针指向的位置是不可知的(随机的、不正确的、没有明确限制的)
2.1 野指针成因
1. 指针未初始化
#include <stdio.h>
int main() {int* p;//局部变量指针未初始化,默认为随机值*p = 20;return 0;
}
2. 指针越界访问
#include <stdio.h>
int main()
{int arr[10] = { 0 };int* p = &arr[0];int i = 0;for (i = 0; i <= 11; i++) {//当指针指向的范围超出数组范围时,p就是野指针*(p++) = i;}return 0;
}
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 指针初始化
#ifdef __cplusplus#define NULL 0#else#define NULL ((void *)0)#endif
初始化如下:
#include <stdio.h>
int main()
{int num = 10;int* p1 = #int* p2 = NULL;return 0;
}
2.2.2 小心指针越界
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 < 10; i++){*(p++) = i;}//此时p已经越界了,可以把p置为NULLp = NULL;//下次使⽤的时候,判断p不为NULL的时候再使⽤//...p = &arr[0];//重新让p获得地址if (p != NULL) //判断{//...}return 0;
}
2.2.4 避免返回局部变量的地址
3. assert 断言
assert.h 头文件定义了宏 assert() ,用于在运行时确保程序符合指定条件,如果不符合,就报错终止运行。这个宏常常被称为“断言”。
assert(p != NULL);
上面代码在程序运行到这⼀行语句时,验证变量 p 是否等于 NULL 。如果确实不等于 NULL ,程序继续运行,否则就会终止运行,并且给出报错信息提示。
assert() 宏接受⼀个表达式作为参数。如果该表达式为真(返回值非零), assert() 不会产生任何作用,程序继续运行。如果该表达式为假(返回值为零), assert() 就会报错,在标准错误流 stderr 中写入⼀条错误信息,显示没有通过的表达式,以及包含这个表达式的文件名和行号。
# define NDEBUG# include <assert.h>
4. 指针的使用和传址的调用
4.1 strlen的模拟实现
size_t strlen ( const char * str );
参数str接收⼀个字符串的起始地址,然后开始统计字符串中 \0 之前的字符个数,最终返回长度。 如果要模拟实现只要从起始地址开始向后逐个字符的遍历,只要不是 \0 字符,计数器就+1,这样直到 \0 就停止。
int my_strlen(const char* str)
{int count = 0;assert(str);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;
}
我们发现其实没产生交换的效果,这是为什么呢?
看一下调试结果:
我们发现在main函数内部,创建了a和b,a的地址是0x00fcfe60,b的地址是0x00fcfe54,在调swap1函数时,将a和b传递给了swap1函数,在swap1函数内部创建了形参x和y接收a和b的值,但是x的地址是0x00fcfd7c,y的地址是0x00fcfd80,x和y确实接收到了a和b的值,不过x的地址和a的地址不⼀样,y的地址和b的地址不⼀样,相当于x和y是独立的空间,那么在swap1函数内部交换x和y的值, 自然不会影响a和b,当swap1函数调用结束后回到main函数,a和b的没法交换。swap1函数在使用的时候,是把变量本身直接传递给了函数,这种调用函数的方式我们之前在函数的时候就知道了,这种叫传值调用。
#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;
}
运行结果:

完。