Suppose I have the following code:
my_struct_member_type *foo() {
volatile my_struct *s = (my_struct *)SOME_ADDRESS;
return &(s->struct_member);
}
Is the pointer returned by foo also volatile?
EDIT: A followup: is the pointer &(s->struct_member) volatile inside of foo?
Yes.
For
volatile my_struct *ssays that the object you see through it isvolatileand all members that you access through this pointer inherit thevolatilequalification.Having the address of a pointer to
volatilereturned if the return type doesn’t say so is a constraint violation and your compiler must have given you a diagnostic for that.Edit: There also seems to be confusion to where the
volatilekeyword applies. In your example it applies to the object pointed to, not to the pointer itself. As a rule of thumb always write the qualifiers (constorvolatile) as far to the right as possible without a type change. Your example then would read:which is entirely different from
where the pointer itself is
volatilebut not the object behind it.Edit 2: Since you ask for my sources, the actual C standard, C11, 6.8.6.4 it says about the
returnstatement:So 6.15.16.1 for the assignment operator what is expected from the conversion:
Here the pointed type on the right has the
volatilequalifier in addtion to the one on the left.