I have a small problem with C syntax. I know that writing
volatile char * volatile foo;
creates a volatile pointer variable to a volatile char.
In my understanding the first volatile means that the pointer points to a volatile element because of the part "volatile char". The second volatile means that the pointer "foo" itself is volatile.
Is this assumption correct?
What will
static volatile char * volatile bar;
do?
Assumed that the above statement is correct it should declare a volatile pointer to an volatile+static char. My problem with that is, that it is not relevant for a pointer to know whether the destination variable is static or not. So this probably declares a static+volatile pointer to a volatile char.
However, assumed this is correct the first volatile would be referring to the pointer and the second one to the data pointed at.
Which is the correct assumption? What does each volatile do?
Thanks
In my understanding the first volatile means that the pointer points to a volatile element because of the part "volatile char". The second volatile means that the pointer "foo" itself is volatile. Is this assumption correct?
Yes. Every type qualifier (volatile
and/or const
and/or restrict
) on the left side of the *
refers to the pointed-at type, while every type qualifier on the right side refers to the pointer type itself. This is only true for type qualifiers.
static
(as well as extern, auto etc) are storage-class specifiers, that are only concerned with variable duration and scope. They always refer to the declared variable type itself, in this case the pointer. It wouldn't make sense to declare a storage-class specifier for the pointed-at data, since that data is not declared on this line, but at some other line.
Also note that you can always declare a pointer variable with more type qualifiers than the pointed-at type has, but never with less. This is important to know when implementing const correctness.