char * const 和 const char * 有什么区别?
有什么区别:
char * const
和
const char *
What's the difference between:
char * const
and
const char *
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
区别在于
const char *
是指向const char
的指针,而char * const
是指向char 的常量指针
。首先,所指向的值不能更改,但指针可以。 第二,指向的值可以改变,但指针不能(类似于引用)。
还有一个
which是一个指向常量char的常量指针(所以它的任何内容都不能改变)。
注意:
以下两种形式是等效的:
and
其确切原因在 C++ 标准中进行了描述,但请务必注意并避免混淆。 我知道几个编码标准更喜欢:
over
(有或没有指针),以便 const 元素的位置与指针 const 的位置相同。
The difference is that
const char *
is a pointer to aconst char
, whilechar * const
is a constant pointer to achar
.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).
There is also a
which is a constant pointer to a constant char (so nothing about it can be changed).
Note:
The following two forms are equivalent:
and
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:
over
(with or without pointer) so that the placement of the
const
element is the same as with a pointerconst
.为了避免混淆,请始终附加 const 限定符。
To avoid confusion, always append the const qualifier.
const
总是修改它前面的东西(它的左边),除非它是类型声明中的第一个东西,它修改它后面的东西(它的右边) )。所以这两个是相同的:
它们定义指向 const int 的指针。 您可以更改
i1
和i2
指向的位置,但无法更改它们指向的值。这:
定义了一个指向整数的 const 指针,并将其初始化为指向内存位置 12345678。您可以更改地址 12345678 处的 int 值,但不能更改
i3
指向的地址。const
always modifies the thing that comes before it (to the left of it), EXCEPT when it's the first thing in a type declaration, where it modifies the thing that comes after it (to the right of it).So these two are the same:
they define pointers to a
const int
. You can change wherei1
andi2
points, but you can't change the value they point at.This:
defines a
const
pointer to an integer and initializes it to point at memory location 12345678. You can change theint
value at address 12345678, but you can't change the address thati3
points to.经验法则:从右到左阅读定义!
const int *foo;
表示“
foo
指向 (*
) 一个无法更改的int
(常量
)”。对于程序员来说,这意味着“我不会更改
foo
指向的值”。*foo = 123;
或foo[0] = 123;
无效。foo = &bar;
。int *const foo;
表示“
foo
无法更改 (const
) 并指向 (*
) 一个int
”。对于程序员来说,这意味着“我不会更改
foo
引用的内存地址”。*foo = 123;
或foo[0] = 123;
。foo = &bar;
无效。const int *const foo;
表示“
foo
无法更改 (const
) 并将 (*
) 指向 < code>int 无法更改 (const
)”。对于程序员来说,这意味着“我不会更改
foo
指向的值,也不会更改foo 的地址
指的是“。*foo = 123;
或foo[0] = 123;
无效。foo = &bar;
无效。Rule of thumb: read the definition from right to left!
const int *foo;
Means "
foo
points (*
) to anint
that cannot change (const
)".To the programmer this means "I will not change the value of what
foo
points to".*foo = 123;
orfoo[0] = 123;
would be invalid.foo = &bar;
is allowed.int *const foo;
Means "
foo
cannot change (const
) and points (*
) to anint
".To the programmer this means "I will not change the memory address that
foo
refers to".*foo = 123;
orfoo[0] = 123;
is allowed.foo = &bar;
would be invalid.const int *const foo;
Means "
foo
cannot change (const
) and points (*
) to anint
that cannot change (const
)".To the programmer this means "I will not change the value of what
foo
points to, nor will I change the address thatfoo
refers to".*foo = 123;
orfoo[0] = 123;
would be invalid.foo = &bar;
would be invalid.const char*
是指向常量字符的指针char* const
是指向字符的常量指针const char* const
是指向常量字符的常量指针const char*
is a pointer to a constant characterchar* const
is a constant pointer to a characterconst char* const
is a constant pointer to a constant characterconst * char
是无效的 C 代码,没有意义。 也许您想问const char *
和char const *
之间的区别,或者可能是const char *
和 a 之间的区别char * const
?另请参阅:
const * char
is invalid C code and is meaningless. Perhaps you meant to ask the difference between aconst char *
and achar const *
, or possibly the difference between aconst char *
and achar * const
?See also:
const char* x 这里 X 基本上是一个指向常量值的字符指针
char* const x 是指字符指针,该指针是常量,但指向的位置可以改变。
const char* const x是1和2的组合,表示它是一个常量字符指针,指向常量值。
const *char x 将导致编译器错误。 它不能被声明。
char const * x 等于点 1。
经验法则是如果 const 与 var name 一起使用,那么指针将是常量,但指向的位置可以更改,否则指针将指向常量位置,并且指针可以指向另一个位置,但是指向位置内容无法更改。
const char* x Here X is basically a character pointer which is pointing to a constant value
char* const x is refer to character pointer which is constant, but the location it is pointing can be change.
const char* const x is combination to 1 and 2, means it is a constant character pointer which is pointing to constant value.
const *char x will cause a compiler error. it can not be declared.
char const * x is equal to point 1.
the rule of thumb is if const is with var name then the pointer will be constant but the pointing location can be changed , else pointer will point to a constant location and pointer can point to another location but the pointing location content can not be change.
另一个经验法则是检查 const 位于何处:
Another thumb rule is to check where const is:
第一个是语法错误。 之间的区别
也许您的意思是和
在这种情况下,第一个是指向无法更改的数据的指针,第二个是始终指向同一地址的指针。
First one is a syntax error. Maybe you meant the difference between
and
In that case, the first one is a pointer to data that can't change, and the second one is a pointer that will always point to the same address.
许多答案提供了特定的技术、经验法则等来理解变量声明的特定实例。 但有一个通用的技术可以理解任何声明:
A)
根据顺时针/螺旋规则
a
是指向常量字符的指针。 这意味着字符是不变的,但指针可以改变。 即a = "other string";
没问题,但a[2] = 'c';
将无法编译B)
根据规则,
a< /code> 是指向字符的 const 指针。 即您可以执行
a[2] = 'c';
但不能执行a = "other string";
Lots of answer provide specific techniques, rule of thumbs etc to understand this particular instance of variable declaration. But there is a generic technique of understand any declaration:
A)
As per the clockwise/spiral rule
a
is pointer to character that is constant. Which means character is constant but the pointer can change. i.e.a = "other string";
is fine buta[2] = 'c';
will fail to compileB)
As per the rule,
a
is const pointer to a character. i.e. You can doa[2] = 'c';
but you cannot doa = "other string";
我想你的意思是 const char * 和 char * const 。
第一个 const char * 是指向常量字符的指针。 指针本身是可变的。
第二个,char * const 是指向字符的常量指针。 指针不能改变,但它指向的字符可以。
然后是 const char * const ,其中指针和字符不能改变。
I presume you mean const char * and char * const .
The first, const char *, is a pointer to a constant character. The pointer itself is mutable.
The second, char * const is a constant pointer to a character. The pointer cannot change, the character it points to can.
And then there is const char * const where the pointer and character cannot change.
这里有详细的代码解释
Here is a detailed explanation with code
char * const 和 const char *?
const char * p;
// 值不能更改char * const p ;
// 地址不能更改const char * const p;
// 两者都不能更改。char * const and const char *?
const char * p;
// value cannot be changedchar * const p;
// address cannot be changedconst char * const p;
// both cannot be changed.语法:
char *const
属于这种情况。语法:
const datatype *var
或datatype const *var
const char*
属于这种情况。Syntax:
char *const
comes under this case.Syntax:
const datatype *var
ordatatype const *var
const char*
comes under this case.const
修饰符应用于紧邻其左侧的术语。 唯一的例外是,当其左侧没有任何内容时,它适用于紧邻其右侧的内容。这些都是“指向常量
char
的常量指针”的等效方式:const char * const
const char const *
char const * const
char const const *
The
const
modifier is applied to the term immediately to its left. The only exception to this is when there is nothing to its left, then it applies to what is immediately on its right.These are all equivalent ways of saying "constant pointer to a constant
char
":const char * const
const char const *
char const * const
char const const *
两条规则
如果 const 位于 char 和 * 之间,则影响左边的。
如果 const 不在 char 和 * 之间,则影响最近的一个。
例如
字符常量*。 这是一个指向常量 char 的指针。
char * const。 这是一个指向字符的常量指针。
Two rules
If const is between char and *, it will affect the left one.
If const is not between char and *, it will affect the nearest one.
e.g.
char const *. This is a pointer points to a constant char.
char * const. This is a constant pointer points to a char.
我想指出的是,使用
int const *
(或const int *
)并不是指向const int
变量的指针,但是对于这个特定的指针来说,这个变量是 const 。例如:
上面的代码编译得很好。
_p
指向一个const
变量,尽管var
本身不是常量。I would like to point out that using
int const *
(orconst int *
) isn't about a pointer pointing to aconst int
variable, but that this variable isconst
for this specific pointer.For example:
The code above compiles perfectly fine.
_p
points to aconst
variable, althoughvar
itself isn't constant.我记得在关于 C 的捷克书上:阅读从变量开始并向左走的声明。
因此,
您可以读作:“
a
是指向char
的常量指针类型的变量”,您可以读作:“
a
是一个指针char 类型的常量变量我希望这会有所帮助:您
将读到
a
是指向 char 类型的常量变量。I remember from Czech book about C: read the declaration that you start with the variable and go left.
So for
you can read as: "
a
is variable of type constant pointer tochar
",you can read as: "
a
is a pointer to constant variable of type char. I hope this helps.Bonus:
You will read as
a
is constant pointer to constant variable of type char.