C语言 ---- const关键字的用法
2020-07-17 16:34
225 查看
const的作用是限定一个变量不允许被改变,提高程序的健壮性
一.修饰全局/局部变量
const int n=5; int const n=5;
用const修饰的变量不允许更改,一定要给变量初始化,否则之后就不能再进行赋值了。
二.常量指针
const int * n; int const * n;
常量指针是指针指向的内容是常量,不能通过这个指针改变变量的值,但是可以通过其他的引用来改变变量的值。
int a=5; const int* n=&a; a=6;
也以指向其他的地址。
int a=5; int b=6; const int* n=&a; n=&b;
三.指针常量
int *const n;
不能在指向其他的地址,但是地址中保存的数值是可以改变的,可以通过其他指向改地址的指针来修改。
int a=5; int *p=&a; int* const n=&a; *p=8;
四.修饰函数的参数
1、防止修改指针指向的内容
void StringCopy(char *strDestination, const char *strSource);
2、防止修改指针指向的地址
void swap ( int * const p1 , int * const p2 )
参考文献:https://blog.csdn.net/xingjiarong/article/details/47282255
相关文章推荐
- C语言关键字const用法
- C语言中const关键字的用法
- C语言中关键字const、static、volatile的用法分析
- 详解C语言中const关键字的用法
- C语言中const关键字的用法大全(包含与#define的区别)
- C语言中const关键字的用法
- C语言中const关键字的用法
- C语言中const关键字的用法
- 详解C语言中const关键字的用法
- C语言中const关键字的用法以及面试技巧
- C语言const关键字的四种用法
- const关键字及其作用(用法),C语言const详解
- const关键字的用法
- static,const,auto各个关键字的作用域和用法
- C语言在8051单片机上的扩展(interrupt、using关键字的用法)
- C语言中的关键字const
- const关键字的深度用法
- C++ const关键字的实例用法
- C语言中static关键字的几种用法总结
- C语言基础(8)-const,volatile,register关键字