How do you declare a particular member of a struct as volatile?
Should be pretty straight forward according to this article:
Finally, if you apply volatile to a struct or union, the entire contents of the struct/union are volatile. If you don't want this behavior, you can apply the volatile qualifier to the individual members of the struct/union.
Exactly the same as non-struct
fields:
#include <stdio.h>
int main (int c, char *v[]) {
struct _a {
int a1;
volatile int a2;
int a3;
} a;
a.a1 = 1;
a.a2 = 2;
a.a3 = 3;
return 0;
}
You can mark the entire struct
as volatile by using "volatile struct _a {...}"
but the method above is for individual fields.
Just a warning on the C/C++ volatile keyword.
Unless you know what you are doing you should never use it.
C/C++ volatile != java/C# volatile
volatile does not help in threaded code unless you really know what you are doing, you need to use C++0x atomic template (or something similar).
The only time I've had to use volatile is when controlling hardware registers.
If the members declared as volatile are not changed due to servicing some interrupt (i.e. a signal handler or other (near) atomic variable), you should probably not be using volatile (as most compilers will optimize it away if its not near enough).
Otherwise, as others have said .. just use it then examine the asm dumps to see if your compiler actually agrees with you :)
In some cases, i.e. certain versions of gcc .. its worth checking those dumps.