我有这个功能
uint8_t Authorization_getRole (char const* userId, UsertoRole_T const *roleTable)
在主程序中我有:
given_Role = Authorization_getRole (userId, roleTable)
我想用函数指针替换函数调用:
uint8_t (*getRole_ptr)()
given_Role = &getRole_ptr;
我的问题是:
在哪里初始化函数指针 getRole_ptr?
如何初始化函数指针?
下面的语法正确吗?
getRole_ptr = Authorization_getRole (userId, roleTable)
我总是推荐带有函数指针的 typedef 。然后,你会写:
// Make sure to get the function's signature right here
typedef uint8_t (*GetRole_Ptr_T)(char const*, UsertoRole_T const*);
// Now initialize your pointer:
GetRole_Ptr_T getRole_ptr = Authorization_getRole;
// To invoke the function pointed to:
given_Role = getRole_ptr(userId, roleTable);
关于“我在哪里初始化函数指针getRole_ptr?”:取决于您的要求。您可以在声明指针时执行此操作,就像我在示例中所做的那样,或者您可以稍后通过分配给它来更改指针:
getRole_ptr = Some_function_with_correct_signature;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)