Search code examples
cstructvolatile

In C, how do you declare the members of a structure as volatile?


How do you declare a particular member of a struct as volatile?


Solution

  • 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.