c - 类型限定符(const)和复杂声明

标签 c pointers constants variable-declaration

假设我们有这样的声明:

int **const*** k;

然后,它可以很好地按字面翻译(根据 cdecl.org)为

declare k as pointer to pointer to pointer to const pointer to pointer to int

但是,我仍然不确定它不允许什么?它限制了哪些操作?我们还可以吗

(***k)++

也就是说,那里加const有什么作用?

还有...同样的问题

int *const**const* k;

这会带来什么不同?

谢谢!

最佳答案

still, I'm not sure to understand what it does not permit?

它不允许修改***k指向的内容。

Could we still do

(***k)++   

。你不能。这是因为 ***K 是一个指向 const 指针的指针的指针,您无法修改它。但是,对 K*k**k 的修改是有效的。

为了方便起见,您可以这样理解:

int *const k;     // k is a const pointer to integer. No modification to k.  
int *const *k;    // *k is a const pointer to integer. No modification to *k.  
int *const **k;   // **k is a const pointer to integer. No modification to **k.  
int *const ***k;  // ***k is a const pointer to integer. No modification to ***k.  
int **const ***k; // ***k is a const pointer to integer. No modification to ***k.

关于c - 类型限定符(const)和复杂声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20999692/

相关文章:

c - 如何在Linux中使用串口读取字符

C:通过交换元素数据对链表进行排序

c - 内存错误和指向字符串的指针

c++ - C 将参数作为空指针列表传递给从 LoadLibrary() 导入的函数

c - 在这个简单的 C 程序中分配内存时我哪里出错了?

c++ - const/非常量对象指针的 union

python - 如何在单独的文件中组织所有不同的参数

c - 墨西哥/Matlab : Accessing objectarray which is a member of another object

c++ - 函数指针指向什么?

c++ - 在 C++ 中使用 const 的指针和符号位置