const修饰的指针的指针 [英] const qualifier for pointers to pointers

查看:130
本文介绍了const修饰的指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点麻烦的推论是什么常量,当应用于指针的指针,等等。
即,什么是常量,当你有

 常量美孚**富;

我可以在这里改变**富的东西吗?在 foo的[0] - >巴= 12;

什么:

 常量富***富;
 美孚**常量foo的;


解决方案

您可以使用 CDECL 来了解什么是C声明的意思。

  const int的**富;
声明富为指针的指针const int的

因此​​,你可以改变的指针,而不是它们指向的值。

 为int * const的* const的富;
声明富为const指针常量指针为int

这一点,相反,是一个cosnt指针,指向一个const指针,以一个非const int的:你不能改变尖价值,但它是可以改变的。


C使用的顺时针/螺旋规则,在你的左侧只有一个修饰语的情况下你读的东西由右至左去(的富)变量的一面:

  INT(5)*(4)常量(3)*(2)常量(1)富;

是一个常数(1)指针(2)常数(3)指针(4)一个整数(5)。

  INT(6)const的(5)*(4)常量(3)*(2)常量(1)富;
常量(6)INT(5)*(4)常量(3)*(2)常量(1)富; // [同上]

在这种情况下为常数(1)的指针(2),以恒定(3)的指针(4)为恒定(5)整数(6)[或为整数(5),其是恒定(6)]

I'm having a bit trouble deducing what is const, when applied to pointers to pointers, etc. i.e., what is const when you have

 const Foo **foo;

Can I here change something in **foo ? as in foo[0]->bar = 12;

What about:

 const Foo ***foo;
 Foo **const foo;

解决方案

You could use cdecl to understand what a C declaration means.

const int **foo;
declare foo as pointer to pointer to const int

thus you can change pointers, but not the value they're pointing to.

int * const * const foo;
declare foo as const pointer to const pointer to int

this, instead, is a cosnt pointer, pointing to a const pointer, to a non-const int: you cannot change pointed value, but it can be changed.


C uses the Clockwise/Spiral Rule, in a case where you have only modifiers at the left side of the variable (of foo) you read stuff going from right to left:

int(5) *(4) const(3) *(2) const(1) foo;

foo is a constant(1) pointer(2) to constant(3) pointer(4) to an integer(5).

int(6) const(5) *(4) const(3) *(2) const(1) foo;
const(6) int(5) *(4) const(3) *(2) const(1) foo; // [same as above]

In this case foo is a constant(1) pointer(2) to constant(3) pointer(4) to a constant(5) integer(6) [or to an integer(5) which is constant(6)].

这篇关于const修饰的指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆