我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?
我想知道在赋值、传递给函数等方面的所有注意事项。
我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?
我想知道在赋值、传递给函数等方面的所有注意事项。
当前回答
在我读到C++大师斯科特·梅耶斯(Scott Meyers)的这本书之前,我和你一样心存疑虑。请参阅本书中的第三项,其中他详细介绍了如何使用const。
只需遵循以下建议
如果单词const出现在星号的左侧,则所指向的是常量如果单词const出现在星号的右侧,则指针本身是常量如果常量出现在两侧,则两者都是常量
其他回答
很多人都答对了,我会在这里整理好,并在给出的答案中添加一些缺失的额外信息。
Const是C语言中的关键字,也称为限定符。Const罐应用于任何变量的声明,以指定其值不会改变
const int a=3,b;
a=4; // give error
b=5; // give error as b is also const int
you have to intialize while declaring itself as no way to assign
it afterwards.
如何阅读?
只需从右到左阅读每一条语句即可顺利完成
3件主要事情
type a. p is ptr to const int
type b. p is const ptr to int
type c. p is const ptr to const int
[错误]
if * comes before int
两种类型
1. const int *
2. const const int *
我们先看
主要类型1。常量int*
在3个地方安排3件事的方法3=6.
i.*开始时
*const int p [Error]
*int const p [Error]
二。开始时常量
const int *p type a. p is ptr to const int
const *int p [Error]
iii.开始时int
int const *p type a.
int * const p type b. p is const ptr to int
主要类型2。常量常量int*
在4个地方安排4件事情的方法,其中2件是相同的4件/2!=12
i.*开始时
* int const const p [Error]
* const int const p [Error]
* const const int p [Error]
二。开始时为int
int const const *p type a. p is ptr to const int
int const * const p type c. p is const ptr to const int
int * const const p type b. p is const ptr to int
iii.启动时的常量
const const int *p type a.
const const * int p [Error]
const int const *p type a.
const int * const p type c.
const * int const p [Error]
const * const int p [Error]
挤成一体
类型a.p是常量int(5)的指针
const int *p
int const *p
int const const *p
const const int *p
const int const *p
类型b.p是int(2)的常量指针
int * const p
int * const const p;
类型c.p是const ptr到const int(2)
int const * const p
const int * const p
只是很少的计算
1. const int * p total arrangemets (6) [Errors] (3)
2. const const int * p total arrangemets (12) [Errors] (6)
小小的额外
int常量*p,p2;
here p is ptr to const int (type a.)
but p2 is just const int please note that it is not ptr
int*常量p,p2;
similarly
here p is const ptr to int (type b.)
but p2 is just int not even cost int
int常量*常量p,p2;
here p is const ptr to const int (type c.)
but p2 is just const int.
完成了
两边带有int的常量将使指针指向常量int:
const int *ptr=&i;
or:
int const *ptr=&i;
*后的const将使常量指针指向int:
int *const ptr=&i;
在这种情况下,所有这些都是指向常量整数的指针,但没有一个是常量指针:
const int *ptr1=&i, *ptr2=&j;
在这种情况下,所有指针都指向常量整数,ptr2是指向常量整数的常量指针。但ptr1不是常量指针:
int const *ptr1=&i, *const ptr2=&j;
这个问题确切地说明了为什么我喜欢用我在问题中提到的在类型id可接受之后是常量的方式做事?
简而言之,我发现记住这个规则最简单的方法是“const”跟在它应用的对象后面。所以在你的问题中,“int const*”表示int是常量,而“int*const”表示指针是常量。
如果有人决定把它放在最前面(例如:“constint*”),作为这种情况下的一个特殊例外,它适用于后面的东西。
许多人喜欢使用这个特殊的例外,因为他们认为它看起来更好。我不喜欢它,因为它是一个例外,从而混淆了事情。
一般规则是const关键字应用于它前面的内容。异常,起始常量适用于以下内容。
constint*与intconst*相同,表示“指向常量int的指针”。const int*const与int const*const相同,表示“指向常量int的常量指针”。
编辑:对于该做的和不该做的,如果这个答案还不够,你能更准确地说出你想要什么吗?
const int*-指向常量int对象的指针。
您可以更改指针的值;不能更改指针指向的int对象的值。
const int*const-指向常量int对象的常量指针。
不能更改指针的值,也不能更改指针指向的int对象的值。
int const*-指向常量int对象的指针。
此语句相当于1。constint*-可以更改指针的值,但不能更改指针指向的int对象的值。
实际上,还有第四种选择:
int*const-指向int对象的常量指针。
可以更改指针指向的对象的值,但不能更改指针本身的值。指针将始终指向同一个int对象,但此int对象的值可以更改。
如果你想确定某种类型的C或C++结构,你可以使用David Anderson制定的顺时针/螺旋规则;但不要与罗斯·J·安德森(Ross J.Anderson)制定的安德森规则混淆,这是一个非常独特的规则。