I have a function as follows that processes the information contained in an array of type unsigned char:
unsigned char LRCsimple(unsigned char *p, createLRC , unsigned char length)
{
}
Works great for mostly unsigned char arrays.
Now, I have a signed array and when I use such a function and it works very well, but I have a warning when compiling the code:
> ../src/apptcpipserver.c:102:9: warning: pointer targets in passing argument 1 of 'LRCsimple' differ in signedness [-Wpointer-sign]
if (0x01 == LRCsimple(apptcpipserverData.cRxedData,0x00,(apptcpipserverData.cRxedData[0x02] - 0x02)))
If I want to avoid this warning, I think the optimal solution is to create a function similar to the one above, but for a signed array, as follows:
unsigned char signedLRCsimple(char *p, createLRC , unsigned char length)
{
}
Or is there something else I can do to avoid that warning message?
CodePudding user response:
Strict aliasing rule allows unsigned char and char alias. Therefore you should be able reuse LRCsimple for processing char*.
Therefore signedLRCsimple could be implemented as:
unsigned char signedLRCsimple(char *p, createLRC xxx, unsigned char length)
{
return LRCsimple((unsigned char*)p, xxx, length);
}
To avoid forcing client to change their code to use signedLRCsimple you could use generic selection introduced in C11 in form of _Generic. Typically it is used to select a function pointer basing on the type of first argument of _Generic.
#define LRCsimple(p, xxx, length) \
_Generic((p), unsigned char*: LRCsimple, \
char *: signedLRCsimple)(p, xxx, length)
Whenever LRCsimple is called the generic selection selects between LRCsimple for unsigned char* and signedLRCsimple for char*. For other types an error is raised.
