0

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?

9
  • 3
    Please don't use snippets for languages that aren't JavaScript. It serves no purpose and even harms readability somewhat. Commented Oct 23, 2021 at 16:22
  • 1
    Depending on what is stored in the string and how it is used, you might be able to use a typecast. Commented Oct 23, 2021 at 16:30
  • 1
    @Beta: There are no templates in C. Commented Oct 23, 2021 at 16:46
  • 1
    Edit the question to explain what the function does. The file name (containing “tcp” and “ip” and the routine name (with “LRC” possibly for longitudinal redundancy check) suggestion the routine might be working solely with the bytes of the buffer for network transmission purposes, just as raw bits without regard to their meaning to the caller. In this case, the data should generally be passed as void * or const void *. Internally, the routine would convert the pointer to unsigned char * or const unsigned char * to work with it… Commented Oct 23, 2021 at 16:51
  • 1
    … On the other hand, if the routine performs some function based on the meaning of the data, such as its values as int types, as characters in a character set, as floating-point data, and so on, then it generally ought to take the data as a pointer to its actual type. Commented Oct 23, 2021 at 16:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

C’s aliasing rules may mean the language will support this, but it does not mean it is a good idea. We should find out what OP’s routine is doing and give advice for that particular situation.
@EricPostpischil, that is why I used word "could". OP can decide if this wrapper is appropriate. I guess it would work because the only issue OP complains about are the warnings
Writing code for the purpose of silencing warnings, rather than for the purpose of getting the code right, is a source of bugs. It is bad programming practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.