我如何阅读C ++指针构造?

时间:2012-11-23 23:18:24

标签: c++ pointers

这是一个新手C ++问题。以下两个结构之间有什么区别?

1. const int* const* const x
2. const int**

我如何阅读这些结构?

3 个答案:

答案 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

的指针