我正在尝试执行以下操作:我不确定 - 方法是否正确。或者,我可能必须编写 Wrapper 函数,但有没有更好的方法。此外,将来其他开发人员希望使用来自另一个库、平台的哈希函数,这取决于不同目的的性能要求。
创建的结构:
typedef struct Hashctx{
char inStream[BUFSIZ];
char outHash[MAX_HASH];
int (*Hashing)(char* DstBuf, size_t DstBufSize, \
size_t *olen, char* SrcBuf, size_t SrcBufSize);
}Hashctx;
Hashctx App1;
并尝试初始化如下:
init()
{
#ifdef PLATFORM
App1.Hashing = SHA1sumPlatform;
#elif
App1.Hashing = SHA1sum;
#endif
}
虽然两个函数的参数相同,但返回类型不同。我坚持错误cannot assigned be entity of type ...
和no definition for App1
int SHA1sum(...)
uint32_t SHA1sumPlatform(...)
我试过类型转换也没有解决错误
Hashing = (int)SHA1sumPlatform;
谢谢
在这一行中,Hashing = (int)SHA1sumPlatform;
您正在尝试function pointer
使用int
这不是转换函数指针的正确方法进行转换。
如果您确定这int
是您想要的正确返回类型,请执行以下操作:
typedef int (*HashingFnType)(char* DstBuf, size_t DstBufSize, \
size_t *olen, char* SrcBuf, size_t SrcBufSize);
typedef struct Hashctx{
char inStream[BUFSIZ];
char outHash[MAX_HASH];
HashingFnType Hashing ;
}Hashctx;
init()
{
#ifdef PLATFORM
Hashing = (HashingFnType)SHA1sumPlatform;
#elif
Hashing = (HashingFnType)SHA1sum;
#endif
}
注意:要转换具有不同类型的函数指针,两种类型都应该兼容。在此处阅读更多相关信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句