c - 警告:从不兼容的指针类型初始化 [-Wincompatible-pointer-types]
问题描述
请帮助我确定菜单项的功能指针的问题是给出错误“从不兼容的指针类型初始化”
这是头文件
/**
* Represents a function that can be selected from the list of
* menu_functions - creates a new type called a menu_function.
*/
void displayItems(VmSystem * system);
typedef void (*MenuFunction)(VmSystem *);
/**
* Represents a menu item to be displayed and executed in the program.
**/
typedef struct menu_item
{
char text[MENU_NAME_LEN + NULL_SPACE];
MenuFunction function;
} MenuItem;
void initMenu(MenuItem * menu);
MenuFunction getMenuChoice(MenuItem * menu);
MenuItem menu[NUM_MENU_ITEMS];
主菜单文件
typedef enum boolean
{
FALSE = 0,
TRUE
} Boolean;
void initMenu(MenuItem * menu)
{
/* Strings names of menu items */
char * menu_items[] = {
"Display Items",
"Purchase Items",
"Save and Exit",
"Add Item",
"Remove Item",
"Display Coins",
"Reset Stock",
"Reset Coins",
"Abort Program"
};
菜单项的功能指针
Boolean(*MenuFunction[])(VmSystem *) = {
displayItems, /*Here i got the error */
purchaseItem,
saveStock,
addItem,
removeItem,
displayCoins,
resetStock,
resetCoins,
abortProgram
};
解决方案
你有两个问题。
首先:
typedef void (*MenuFunction)(VmSystem *);
其次是
Boolean(*MenuFunction[])(VmSystem *) = { ... };
两个不同的东西,同一个名字。
然后至于您的错误:您在评论中声明例如displayItem
与函数类型匹配MenuFunction
,并且它返回void
。问题是数组MenuFunction
(看看这有多混乱?)是一个函数返回的数组Boolean
。这两种类型不兼容,必须换一种。
推荐阅读
- dart - Flutter:只显示图像的特殊部分
- macros - 匹配封闭函数的返回类型
- javascript - 使用 Webpacker 在 Rails 应用程序中渲染 ReactJS CRUD 组件的问题
- android - 有没有办法在安卓上检查电子邮件域的有效性?
- java - Hibernate 将 null 而不是使用来自 posrgresql 的序列
- java - 如何拆分没有模式的文本文件
- javascript - 查询集合并将数据作为列表返回的更简洁的方法?
- ios - UIBarButtonItem 的 customView 可访问性
- angular - 协议的 ngx-mqtt 服务选项错误
- java - 为什么 Jersey 不允许使用不同 HttpMethods 的相同休息 url?