Explicitly declare a function pointer, not a data pointer

I am converting a program from C ++ to C using Microsoft Visual Studio. This is a Windows application, but I do not think this is relevant to this issue.

I use the /W4 switch to turn on additional warning messages. I would like my program to compile without warning. The program compiles and works fine. These are only warning messages.

Before the program was C ++, it was compiled without warning, but now that I have converted it to C as best as possible, a warning appears that I do not know how to deal with it.

Here is the relevant code. The idea is that I am creating a menu system for the graphical interface. Each menu contains a set of menu items, and each menu item contains a pointer to a function that represents the action that will be performed when the user selects this menu item.

 typedef struct MENUITEM { wchar_t* Name; void* Action; } MENUITEM; typedef struct MENU { wchar_t* Name; uint8_t SelectedItem; uint8_t ItemCount; MENUITEM** Items; } MENU; 

Elsewhere in the program, this code processes an event when the user selects a menu item:

 void(*FP) (void) = NULL; FP = (void(*)())MyMenu.Items[MyMenu.SelectedItem]->Action; FP(); 

Here are the warnings generated by the compiler:

C4152 custom extension, conversion of function / data pointer to expression

and

C4113 'void (__cdecl *) ()' differs in the parameter lists from 'void (__cdecl *) (void)'

The MSDN documentation for warning message C4152 states:

The function pointer is converted to or from a data pointer. This conversion is permitted in Microsoft extensions (/ Ze), but not under ANSI C.

OK, so I think this explains why it did not give a warning like C ++, but when compiling like C it does it. But I don’t know how to deal with it. Honestly, so far I have not understood that there is a difference between a function pointer and a data pointer.

I cannot figure out how to change the declaration of my function pointer so that it is explicit to the compiler that it is a function pointer, not a data pointer.

+5
source share
2 answers

You forgot void in the role. This is what the C4113 says.

You can completely exclude by declaring Action as a function pointer:

 void (*Action)(void); 
+3
source

You probably need this (using typedef ):

 typedef void(*FP) (void); typedef struct MENUITEM { wchar_t* Name; FP Action; } MENUITEM; int main() { MENUITEM it; // ... more code FP fp = it.Action; fp(); return 0; } 
+5
source

Source: https://habr.com/ru/post/1240256/


All Articles