Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Declare volatile pointer to function

Tags:

c

volatile

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

like image 396
osgx Avatar asked Feb 02 '11 17:02

osgx


People also ask

Can a pointer be volatile in C?

Yes, a pointer can be volatile if the variable that it points to can change unexpectedly even though how this might happen is not evident from the code. An example is an object that can be modified by something that is external to the controlling thread and that the compiler should not optimize.

Can functions be volatile?

Volatile functions are functions in which the value changes each time the cell is calculated. The value can change even if none of the function's arguments change. These functions recalculate every time Excel recalculates. For example, imagine a cell that calls the function NOW .

What is the use of volatile pointer?

The const keyword specifies that the pointer cannot be modified after initialization; the pointer is protected from modification thereafter. The volatile keyword specifies that the value associated with the name that follows can be modified by actions other than those in the user application.

What is volatile keyword in C with example?

The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.


2 Answers

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.

like image 57
Jerry Coffin Avatar answered Nov 10 '22 15:11

Jerry Coffin


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

like image 40
Carl Norum Avatar answered Nov 10 '22 16:11

Carl Norum