[英]How to compile a C99 source on vc++2008 but without changing the original function?
如:
enum {
SPICE_MSG_CURSOR_INIT = 101,
SPICE_MSG_CURSOR_RESET,
SPICE_MSG_CURSOR_SET,
SPICE_MSG_CURSOR_MOVE,
SPICE_MSG_CURSOR_HIDE,
SPICE_MSG_CURSOR_TRAIL,
SPICE_MSG_CURSOR_INVAL_ONE,
SPICE_MSG_CURSOR_INVAL_ALL,
SPICE_MSG_END_CURSOR
};
static const spice_msg_handler cursor_handlers[] = {
[ SPICE_MSG_CURSOR_INIT ] = cursor_handle_init,
[ SPICE_MSG_CURSOR_RESET ] = cursor_handle_reset,
[ SPICE_MSG_CURSOR_SET ] = cursor_handle_set,
[ SPICE_MSG_CURSOR_MOVE ] = cursor_handle_move,
[ SPICE_MSG_CURSOR_HIDE ] = cursor_handle_hide,
[ SPICE_MSG_CURSOR_TRAIL ] = cursor_handle_trail,
[ SPICE_MSG_CURSOR_INVAL_ONE ] = cursor_handle_inval_one,
[ SPICE_MSG_CURSOR_INVAL_ALL ] = cursor_handle_inval_all,
};
我什至不明白這是什么意思。 在vc ++ 2008上無法通過,如何更改?
這就是C99中引入的指定初始化器 。 例如,要初始化數組,可以按任何順序指定數組索引:
int a[6] = { [4] = 29, [2] = 15 };
相當於
int a[6] = { 0, 0, 15, 0, 29, 0 };
因此,在您的示例中,使用枚舉的定義,將cursor_handlers
初始化為包含101
元素的0
,然后是其他幾個值。
問題是,VC ++ 2008不支持大多數C99功能,包括此功能。 因此,您必須以固定順序初始化數組(舊的C89方法),或者以全0
初始化數組並分配適當的元素。
不幸的是,數組是const
,所以您必須使用C89方式:
static const spice_msg_handler cursor_handlers[] = {0, 0, /*...all 101 0s*/, cursor_handle_init, /*...*/}
如果不是const
,則可以使用以下命令:
static spice_msg_handler cursor_handlers[SPICE_MSG_END_CURSOR] = {};
cursor_handlers[SPICE_MSG_CURSOR_INIT] = cursor_handle_init;
cursor_handlers[SPICE_MSG_CURSOR_RESET] = cursor_handle_reset;
//...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.