Am trying do follows: I am not sure - is it right way. Alternatively I may have to write Wrapper function, But like to is theer any better way. Also, in future other developer would like to use hash function from another library, platform, depends on performance requirement for different purposes.
Created structure:
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;
And trying to initialize as follows:
init()
{
#ifdef PLATFORM
App1.Hashing = SHA1sumPlatform;
#elif
App1.Hashing = SHA1sum;
#endif
}
Although the arguments taken by both functions are same, the return type is different. I am stuck with error cannot assigned be entity of type ...
and no definition for App1
int SHA1sum(...)
uint32_t SHA1sumPlatform(...)
I tried typecasting also not resolving the error
Hashing = (int)SHA1sumPlatform;
Thank you
In this line Hashing = (int)SHA1sumPlatform;
You are trying to cast a function pointer
with int
which is not the correct way of casting a function pointer.
If you are sure that int
is the correct return type you want then do as follows:
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
}
Note : To cast a function pointer with a different types , both types should be compatible. Read More about it here.