这是一个新手C ++问题。以下两个结构之间有什么区别?
1. const int* const* const x
2. const int**
我如何阅读这些结构?
答案 0 :(得分:8)
我如何阅读这些结构?
向后阅读它们并将*
读作“指向”的指针。
const int* const* const
是指向整数常量的常量指针的常量指针。
const int**
是指向整数常量的指针。
答案 1 :(得分:2)
如果你以正确的方式分组,那会更容易一些。例如,*const
实际上是一个意味着“const指向”的单位(您可以在此处阅读const
作为下标:*const
)。我把它写成:
const int *const *const p1; // p1 is a const pointer to const pointer to const int
const int **p2; // p2 is a pointer to pointer to const int
还要记住,声明从内向外读取,从声明的标识符开始。
答案 2 :(得分:2)
解密声明有一个有用/有趣的工具:http://cdecl.ridiculousfish.com/
在您的情况下,它报告:
const int* const* const x
=>将x声明为指向const int的const指针的const指针
const int** x
=>将x声明为指向const int