我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?
我想知道在赋值、传递给函数等方面的所有注意事项。
我总是搞砸如何正确使用const int*、const int*const和int const*。有没有一套规则来定义你能做什么和不能做什么?
我想知道在赋值、传递给函数等方面的所有注意事项。
当前回答
简单助记符:
类型指针<-*->指针对象名称
我喜欢把int*I看作是声明“I的解引用是int”;在这个意义上,constint*i表示“i的deref是constint”,而int*consti表示“consti的derref是int”。
(像这样思考的一个危险是,它可能会导致人们倾向于int const*i风格的声明,而人们可能会讨厌/拒绝这种声明)
其他回答
为了C遵循其他解释的完整性,对C++不确定。
pp-指针到指针p-指针数据-在示例x中指出的内容粗体-只读变量
指针
p数据-int*p;p数据-int常量*p;p数据-int*const p;p数据-int const*const p;
指向指针的指针
pp p数据-int**pp;pp p数据-int**const pp;pp p数据-int*const*pp;pp p数据-int常量**pp;pp p数据-int*const*const pp;pp p数据-int const**const pp;pp p数据-int const*const*pp;pp p数据-int常量*常量*常量pp;
// Example 1
int x;
x = 10;
int *p = NULL;
p = &x;
int **pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 2
int x;
x = 10;
int *p = NULL;
p = &x;
int ** const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
// Example 3
int x;
x = 10;
int * const p = &x; // Definition must happen during declaration
int * const *pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 4
int const x = 10; // Definition must happen during declaration
int const * p = NULL;
p = &x;
int const **pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 5
int x;
x = 10;
int * const p = &x; // Definition must happen during declaration
int * const * const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
// Example 6
int const x = 10; // Definition must happen during declaration
int const *p = NULL;
p = &x;
int const ** const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
// Example 7
int const x = 10; // Definition must happen during declaration
int const * const p = &x; // Definition must happen during declaration
int const * const *pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 8
int const x = 10; // Definition must happen during declaration
int const * const p = &x; // Definition must happen during declaration
int const * const * const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
N级取消引用
继续前进,但愿人类将你逐出教会。
int x = 10;
int *p = &x;
int **pp = &p;
int ***ppp = &pp;
int ****pppp = &ppp;
printf("%d \n", ****pppp);
就像每个人都指出的那样:
常量X*p、X*constp和常量X*const p之间有什么区别?
必须读取指针声明从右到左。const X*p表示“p指向常量的X”:X对象不能通过p更改。X*const p表示“p是指向非常量X的常量指针”:不能更改指针p本身,但可以通过p更改X对象。const X*const p表示“p是指向常量X的常量指针”:不能更改指针p本身,也不能通过p更改X对象。
这很简单,但很棘手。请注意,我们可以将const限定符应用于任何数据类型(int、char、float等)。
让我们看看下面的例子。
const int*p==>*p是只读的[p是指向常量整数的指针]
int const*p==>*p是只读的[p是指向常量整数的指针]
int*p const==>错误语句。编译器抛出语法错误。
int*const p==>p是只读的[p是指向整数的常量指针]。因为这里的指针p是只读的,所以声明和定义应该在同一位置。
const int*p const==>错误语句。编译器抛出语法错误。
const int const*p==>*p为只读
const int*const p==>*p和p是只读的[p是指向常量整数的常量指针]。因为这里的指针p是只读的,所以声明和定义应该在同一位置。
int const*p const==>错误语句。编译器抛出语法错误。
int const int*p==>错误语句。编译器抛出语法错误。
int const const*p==>*p是只读的,与int const*p等效
int const*const p==>*p和p是只读的[p是指向常量整数的常量指针]。因为这里的指针p是只读的,所以声明和定义应该在同一位置。
最初的设计者多次将C和C++声明语法描述为失败的实验。
相反,让我们将类型命名为“pointer to type”;我叫它Ptr_:
template< class Type >
using Ptr_ = Type*;
现在Ptr_<char>是一个指向char的指针。
Ptr_<const char>是指向const char的指针。
const Ptr_<const char>是指向const char的const指针。
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)制定的安德森规则混淆,这是一个非常独特的规则。