简体   繁体   English

C:声明指向函数的易失性指针

[英]C: Declare volatile pointer to function

How to declare a pointer to function in C, in order that the pointer itself is volatile. 如何在C中声明一个指向函数的指针,以使指针本身是易失性的。

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. 我在http://wiki.answers.com/Q/Volatile_example_code_sample_coding_of_volatile_pointer上阅读了关于volatile指针的内容。

There are sometimes problems with volatile pointers and pointer-to volatile: 有时挥发性指针和指向volatile的问题:

Now, it turns out that pointers to volatile variables are very common. 现在,事实证明指向volatile变量的指针非常常见。 Both of these declarations declare foo to be a pointer to a volatile integer: 这两个声明都声明foo是一个指向volatile变量的指针:

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. 所以,我希望获得一个易失性指针,而不是指向“volatile”函数的指针。

Thanks 谢谢

Think of the asterisk as a "barrier". 将星号视为“障碍”。 Qualifiers ( const or volatile ) closer to the variable name than the asterisk modify the pointer itself. 更接近变量名称的限定符( constvolatile )比星号修改指针本身。 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: 当然,除了你需要parens来定义一个函数指针而不是声明一个返回指针的函数:

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

static is a storage class rather than a qualifier, so the same is not true in its case. static是一个存储类而不是限定符,因此在它的情况下也不是这样。 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". 没有“指向extern int”或“指向static int”的指针,只有“指向int的指针”。 If you specify a storage class ( static or extern ), it always comes first . 如果指定存储类( staticextern ),它始终是第一个

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. 其他线程已经讨论了线程和volatile之间的关系,所以我不会在此重复,除非注意到这可能没有用。

cdecl comes in really handy for this sort of problem: cdecl对于这类问题非常方便:

$ 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 cdecl的来源: http//cdecl.org/files/cdecl-blocks-2.5.tar.gz

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM