const与指针

 C/C++中的关键字,CONST很湿让人困惑的,所以总结了一下在网上看到的文章,留作笔记。(注意:C++与C中用略微不同)

 

Just like normal variables, pointers can be declared constant. There are two different ways that pointers and const can be intermixed, and they are very easy to mix up.

To declare a const pointer, use the const keyword between the asterisk and the pointer name:

1
2
int nValue = 5;
int * const pnPtr = &nValue;

Just like a normal const variable, a const pointer must be initialized to a value upon declaration, and its value can not be changed. This means a const pointer will always point to the same value. In the above case, pnPtr will always point to the address of nValue. However, because the value being pointed to is still non-const, it is possible to change the value being pointed to via dereferencing the pointer:

1
*pnPtr = 6; // allowed, since pnPtr points to a non-const int

It is also possible to declare a pointer to a constant variable by using the const before the data type.

1
2
int nValue = 5;
const int *pnPtr = &nValue;

Note that the pointer to a constant variable does not actually have to point to a constant variable! Instead, think of it this way: a pointer to a constant variable treats the variable as constant when it is accessed through the pointer.

Thus, the following is okay:

1
nValue = 6; // nValue is non-const

But the following is not:

1
*pnPtr = 6; // pnPtr treats its value as const

Because a pointer to a const value is a non-const pointer, the pointer can be redirected to point at other values:

1
2
3
4
5
int nValue = 5;
int nValue2 = 6;
  
const int *pnPtr = &nValue;
pnPtr = &nValue2; // okay

Confused? To summarize:

  • A non-const pointer can be redirected to point to other addresses.
  • A const pointer always points to the same address, and this address can not be changed.
  • A pointer to a non-const value can change the value it is pointing to.
  • A pointer to a const value treats the value as const (even if it is not), and thus can not change the value it is pointing to.

Finally, it is possible to declare a const pointer to a const value:

1
2
const int nValue;
const int * const pnPtr = &nValue;

A const pointer to a const value can not be redirected to point to another address, nor can the value it is pointing to be changed.

Const pointers are primarily used for passing variables to functions. We will discuss this further in the section on functions.

 

另外,来自百度百科的,写的也很好。

 

const是一个C语言的关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提高程序的安全性和可靠性,另外,在观看别人代码的时候,清晰理解const所起的作用,对理解对方的程序也有一些帮助。另外CONST在其他编程语言中也有出现,如C++、PHP5、C#.net、HC08 C。

  C中CONST的使用:
  虽然这听起来很简单,但实际上,const的使用也是c语言中一个比较微妙的地方,微妙在何处呢?请看下面几个问题。
  问题:const变量 & 常量
  为什么下面的例子在使用一个const变量来初始化数组,ANSI C的编译器会报告一个错误呢? 
  const int n = 5;
  int a[n];
  答案与分析:
  1)、这个问题讨论的是“常量”与“只读变量”的区别。常量肯定是只读的,例如5, "abc",等,肯定是只读的,因为常量是被编译器放在内存中的只读区域,当然也就不能够去修改它。而“只读变量”则是在内存中开辟一个地方来存放它的值,只不过这个值由编译器限定不允许被修改。C语言关键字const就是用来限定一个变量不允许被改变的修饰符(Qualifier)。上述代码中变量n被修饰为只读变量,可惜再怎么修饰也不是常量。而ANSI C规定数组定义时长度必须是“常量”,“只读变量”也是不可以的。
  2)、注意:在ANSI C中,这种写法是错误的,因为数组的大小应该是个常量,而const int n,n只是一个变量(常量 != 不可变的变量,但在标准C++中,这样定义的是一个常量,这种写法是对的),实际上,根据编译过程及内存分配来看,这种用法本来就应该是合理的,只是 ANSI C对数组的规定限制了它。
  3)、那么,在ANSI C 语言中用什么来定义常量呢?答案是enum类型和#define宏,这两个都可以用来定义常量。
  问题:const变量 & const 限定的内容
  下面的代码编译器会报一个错误,请问,哪一个语句是错误的呢? 
  typedef char * pStr;
  char string[4] = "abc";
  const char *p1 = string;
  const pStr p2 = string;
  p1++;
  p2++;
  答案与分析:
  问题出在p2++上。
  1)、const使用的基本形式: const char m;
  限定m不可变。
  2)、替换1式中的m, const char *pm;
  限定*pm不可变,当然pm是可变的,因此问题中p1++是对的。
  3)、替换1式char, const newType m;
  限定m不可变,问题中的pStr就是一种新类型,因此问题中p2不可变,p2++是错误的。
  问题:const变量 & 字符串常量
  请问下面的代码有什么问题?
  char *p = "i'm hungry!";
  p[0]= 'I';
  答案与分析:
  上面的代码可能会造成内存的非法写操作。分析如下, "i'm hungry"实质上是字符串常量,而常量往往被编译器放在只读的内存区,不可写。p初始指向这个只读的内存区,而p[0] = 'I'则企图去写这个地方,编译器当然不会答应。
  问题:const变量 & 字符串常量2
  请问char a[3] = "abc" 合法吗?使用它有什么隐患?
  答案与分析:
  在标准C中这是合法的,但是它的生存环境非常狭小;它定义一个大小为3的数组,初始化为"abc",注意,它没有通常的字符串终止符'\0',因此这个数组只是看起来像C语言中的字符串,实质上却不是,因此所有对字符串进行处理的函数, 比如strcpy、printf等,都不能够被使用在这个假字符串上。
  问题5:const & 指针
  类型声明中const用来修饰一个常量,有如下两种写法,那么,请问,下面分别用const限定不可变的内容是什么?
  1)、const在前面
  const int nValue; //nValue是const
  const char *pContent; //*pContent是const, pContent可变
  const (char *) pContent;//pContent是const,*pContent可变
  char* const pContent; //pContent是const,*pContent可变
  const char* const pContent; //pContent和*pContent都是const
  2)、const在后面,与上面的声明对等
  int const nValue; // nValue是const
  char const * pContent;// *pContent是const, pContent可变
  (char *) const pContent;//pContent是const,*pContent可变
  char* const pContent;// pContent是const,*pContent可变
  char const* const pContent;// pContent和*pContent都是const
  答案与分析:
  const和指针一起使用是C语言中一个很常见的困惑之处,在实际开发中,特别是在看别人代码的时候,常常会因为这样而不好判断作者的意图,下面讲一下我的判断原则:
  当const所在代码段中不包含括号时,沿着*号划一条线,如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量;如果const位于*的右侧,const就是修饰指针本身,即指针本身是常量。你可以根据这个规则来看上面声明的实际意义,相信定会一目了然。
  另外,需要注意:对于const (char *) ; 因为char *是一个整体,相当于一个类型(如 char),因此,这时限定指针是const。
  一个简单的判断方法:指针运算符*,是从右到左,那么如:char const * pContent,可以理解为char const (* pContent),即* pContent为const,而pContent则是可变的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值