开发者

C: Declare volatile pointer to function

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 (const or volatile) 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:

static void * volatile f_pointer(void *);

Except, of course, that you need parens to define a pointer to a function instead of declaring a function returning a pointer:

static void (*volatile f_pointer)(void *);

static is 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 (static or extern), it always comes first.

Other threads have discussed the relationship between threading and volatile so I won't repeat that here beyond noting that this probably won't be useful.


cdecl comes in really handy for this sort of problem:

$ cdecl
Type `help' or `?' for help
cdecl> declare f_pointer as static volatile pointer to function(pointer to void) returning void
static void (* volatile f_pointer)(void *)
cdecl>

Source of cdecl: http://cdecl.org/files/cdecl-blocks-2.5.tar.gz


static void (* volatile f_pointer)(void*);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜