const 的主要用法
2011-03-28 20:33
176 查看
const 主要是为了增强程序的健壮性,减少程序少出错。
const 修饰的值是常量,在C语言中更精确的说是只读的变量,在编译时是不能改变的。
编译器通常不为普通const 只读变量分配存储空间
const int M=1; //此时并未将m 放入内存中
const int a=100;
同时也可以这样修饰int const a=100;
修饰数组
定义或说明一个只读数组可采用如下格式:
int const a[5]={1, 2, 3, 4, 5};或
const int a[5]={1, 2, 3, 4, 5};
对指针的修饰
const int *p;//p可变,p所指向的内容不可变
int const *p;//p可变,p所指向的内容不可变
int *const p;//P不可变,p所指向的内容可变
const int *const p;//p和p所指向的内容均不可变
const 也可以修饰函数参数,主要是为了防止函数体内对参数意外的改变。比如int FUN(const int i);
const 修饰符也可以修饰函数的返回值,返回值不可被改变
如:const int Fun (void)
下面举个例子啊!
const 修饰的值是常量,在C语言中更精确的说是只读的变量,在编译时是不能改变的。
编译器通常不为普通const 只读变量分配存储空间
const int M=1; //此时并未将m 放入内存中
const int a=100;
同时也可以这样修饰int const a=100;
修饰数组
定义或说明一个只读数组可采用如下格式:
int const a[5]={1, 2, 3, 4, 5};或
const int a[5]={1, 2, 3, 4, 5};
对指针的修饰
const int *p;//p可变,p所指向的内容不可变
int const *p;//p可变,p所指向的内容不可变
int *const p;//P不可变,p所指向的内容可变
const int *const p;//p和p所指向的内容均不可变
const 也可以修饰函数参数,主要是为了防止函数体内对参数意外的改变。比如int FUN(const int i);
const 修饰符也可以修饰函数的返回值,返回值不可被改变
如:const int Fun (void)
下面举个例子啊!
相关文章推荐
- const, static, extern, volatile的主要用法
- const、volatile、mutable的用法
- c++中const的用法
- 关于const的定义以及用法
- 嵌入式编程中关于const,static,extern,volatile的用法
- operator 的主要用法
- Const、volatile、mutable的用法
- C/C++中const用法总结
- C/C++语言中const的用法
- php const用法详解
- const用法详解
- xmemcached主要用法及与Spring集成--总结
- (1.1.6.1)一个例子让你搞懂“指针和引用”以及const的用法、空指针
- const的常见用法
- 符号“&”和const在函数里不同位置的用法
- const修饰规则 及其 用法
- const关键字的用法
- const 作用和用法
- 详解C语言中const关键字的用法
- C++中const用法总结