[c ++ const指向指针的指针

问题描述 投票:0回答:1

指向常数int的指针。我可以更改指针,但不能更改值:

const int* a;

指向int的常量指针。我可能不会更改指针,但可能会更改变量的值:

int* const a;

现在,如果我正在处理指向指针的指针,情况会如何?

int** a;

我如何:

a)声明指向非常量int的非常量指针的const指针

b)声明一个指向非常量int的const指针的非常量指针

c)声明一个指向const int的非const指针的非const指针?

c++ pointers const const-correctness
1个回答
0
投票

a)声明指向非常量int的非常量指针的const指针

int ** const a = nullptr;

请注意,由于指针为const,因此必须在声明中进行初始化。

b)声明一个指向非常量int的const指针的非常量指针

int * const *b;   

c)声明一个指向const int的非const指针的非const指针

const int **c;

0
投票

关于const相对于*的位置:如果const is on the left then what's pointing is const, on the right then what's pointer to isconst`。

© www.soinside.com 2019 - 2024. All rights reserved.