How to declare a pointer to function in C, in order that the pointer itself is volatile.
static void volatile (* f_pointer)(void*);
static void (volatile * f_pointer)(void*);
static void (* volatile f_pointer)(void*);
Why I asking this? I read at http://wiki.answers.com/Q/Volatile_example_code_sample_coding_of_volatile_pointer about volatile pointers.
There are sometimes problems with volatile pointers and pointer-to volatile:
Now, it turns out that pointers to
volatile variables are very common.
Both of these declarations declare foo
to be a pointer to a volatile integer:
volatile int * foo;
int volatile * foo;
Volatile pointers to non-volatile variables are very rare (I think I’ve used them once),
but I’d better go ahead and give you the syntax:
int * volatile foo;
So, I want to get a volatile pointer to function not a pointer to “volatile” function.
Thanks
Think of the asterisk as a “barrier”. Qualifiers (
constorvolatile) closer to the variable name than the asterisk modify the pointer itself. Qualifiers farther way from the variable name than the asterisk modify what the pointer will refer to. In this case, therefore, you would have:Except, of course, that you need parens to define a pointer to a function instead of declaring a function returning a pointer:
staticis a storage class rather than a qualifier, so the same is not true in its case. You can only specify a storage class for the variable itself, not what it points at. There’s no such thing as a “pointer to extern int” or “pointer to static int”, only “pointer to int”. If you specify a storage class (staticorextern), it always comes first.Other threads have discussed the relationship between threading and
volatileso I won’t repeat that here beyond noting that this probably won’t be useful.