我有一个包含回调函数指针的结构,如:
typedef void (*LOAD_CB)(resource* r);
typedef void (*UNLOAD_CB)(resource* r);
typedef void (*CREATE_CB)(void* i);
typedef void (*DESTROY_CB)(void* i);
typedef struct /*_resman_callbacks*/ //<-- causes error
{
LOAD_CB load;
UNLOAD_CB unload;
CREATE_CB create;
DESTROY_CB destroy;
} resman_callbacks;
然后我在运行时初始化这些函数,并将它们添加到包含调用函数的其他回调的列表中:
register_extension(".my_file_extension", &(/*error appears here*/resman_callbacks){load, unload, create, destroy});
当我的结构没有名称(例如_resman_callbacks)时,这不会导致错误,但是当结构定义中提供了名称时,我的IDE会显示错误&#34;类型为&#的复合文字34; resman_callbacks&#34;不允许&#34;。这不会导致任何运行时问题,也不会导致代码编译失败。这让我有两个问题
A)考虑到我的代码仍在运作,这是否值得关注?
B)为什么结构具有导致错误的名称?
我的IDE是Visual Studio Express 2013。
编辑: 添加了register_extension的声明和实现,以及其他相关数据。
//resman.h
void register_extension(char* file_ext, resman_callbacks* cb);
//resmain.c
typedef struct
{
char* ext; //extension associated with this resource
resman_callbacks* cb; //structure containing callback functions
} registered_extension;
typedef struct
{
registered_extension* reg_ext; //list of registered extensions
unsigned short ext_ct; //current number of registered extensions
unsigned short size; //maximum number of registered extensions
} ext_manager;
static ext_manager extman;
void register_extension(char* ext, resman_callbacks* callbacks)
{
if (extman.ext_ct == extman.size)
{
extman.size = extman.size * 2;
extman.reg_ext = realloc(extman.reg_ext, extman.size);
}
*(extman.reg_ext + extman.ext_ct) = (registered_extension) { ext, callbacks };
extman.ext_ct = extman.ext_ct + 1;
}
答案 0 :(得分:2)
假设您在之前声明了以下的函数,那么您实际在代码中使用它们:
void load(resource* r);
void unload(resource* r);
void create(void* i);
void destroy(void* i);
试试这个:
resman_callbacks cb = {load, unload, create, destroy};
register_extension(".my_file_extension", &cb);