char * const 和 const char * 有什么区别? [英] What is the difference between char * const and const char *?
问题描述
有什么区别:
char * const
和
const char *
推荐答案
区别在于 const char *
是一个指向 const char
的指针,而 char * const
是一个指向 char
的常量指针.
The difference is that const char *
is a pointer to a const char
, while char * const
is a constant pointer to a char
.
首先,被指向的值不能改变,但指针可以改变.第二种,被指向的值可以改变,但指针不能(类似于引用).
The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).
还有一个
const char * const
这是一个指向常量字符的常量指针(所以它的任何内容都不能改变).
which is a constant pointer to a constant char (so nothing about it can be changed).
注意:
以下两种形式是等价的:
The following two forms are equivalent:
const char *
和
char const *
C++ 标准中描述了这种情况的确切原因,但重要的是要注意并避免混淆.我知道几种更喜欢的编码标准:
The exact reason for this is described in the C++ standard, but it's important to note and avoid the confusion. I know several coding standards that prefer:
char const
结束
const char
(带或不带指针)以便 const
元素的位置与带有指针的 const
相同.
(with or without pointer) so that the placement of the const
element is the same as with a pointer const
.
这篇关于char * const 和 const char * 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!