当有多个间接级别时如何解释 volatile

How to interpret volatile when there are multiple levels of indirection

假设我有以下

struct my_struct ** value;

我明白

的区别
struct my_struct volatile * other_value; // pointer is volatile

struct * volatile other_value; // what pointer points to is volatile

但是

有什么区别
struct my_struct * volatile * value;  // call this (1)

struct my_struct ** volatile value;  // call this (2)

(1) 表示值指向的指针是易失性的,(2) 表示值是易失性的,它指向的指针和指针指向的数据不是易失性的,这样说对吗?或者我把它倒过来?

更普遍地考虑可能看起来像这样的东西

struct my_struct ***** volatile *** value

这"series"个指针中哪个指针是volatile指针?是从值重定向的指针(这是正确的术语吗?)3 次还是 4 次?换句话说,volatile 是否总是在最右边运行 value/ptr/statement(这里的正确术语是什么?)。

例如

struct my_struct ******** volatile value

表示值是易变的

struct my_struct ******* volatile * value

表示指向的指针值是易变的

struct my_struct ****** volatile ** value

表示value指向的指针指向的指针是volatile的。等等。我的理解正确吗?

编辑: 我完全错了。 volatile 适用于左侧而不是右侧。

根据经验,限定符(volatile 或 const 等)应位于其左侧。

I understand the difference between

struct my_struct volatile * other_value; // pointer is volatile

and

struct * volatile other_value; // what pointer points to is volatile
... Or do I have it backwards?

你搞反了。第一种情况使结构数据易变,第二种情况使指针本身易变。

struct my_struct ***** volatile *** value

这意味着左指针* volatile是volatile限定的。

一般来说,从右到左阅读 C 中的声明有助于:

  • other_value other_value...
  • * other_value ...是一个指针...
  • struct my_struct volatile * other_value ... 到 struct my_struct volatile.

另请注意,愚蠢的是,C 允许将变量声明中的限定符放在任何地方。这意味着 volatile int* x;int volatile* x; 是等价的。为什么允许这样做没有合理的理由,它一直都是这样。