Conversion between uint8 and char in C

abdullah cinar picture abdullah cinar · Feb 8, 2016 · Viewed 12.1k times · Source

I have an API that implements a writing operation to EEPROM. Here is its declaration:

CYBLE_API_RESULT_T CyBle_StoreAppData (uint8 * srcBuff, const uint8 destAddr[], uint32 buffLen, uint8 isForceWrite);

It is working well when I call this function and send an array parameter to srcBuff which has been declared as uint8 type.

The problem is, I need to send char array pointer to it. I was thinking that char is already a uint8, but I get a compiler warning if I send a char array pointer to that function instead of uint8. Why can't I use char instead of uint8 ? Here are 2 examples of calling that function:

static const uint8      datastack_ROM[dedicatedRomSize] = {0};
uint8                   Container_ID[10];
char                    Prefix[10];

//Call the function with Container_ID which has been declared as uint8. This is working.
CyBle_StoreAppData(Container_ID,datastack_ROM,10,0);

//Call the function with Prefix which has been declared as char. This is NOT working.
CyBle_StoreAppData(Prefix,datastack_ROM,10,0);

Here is the warning for the second call:

passing char[10] to parameter of type 'uint8 *' converts between pointers to integer types with different sign.

Aren't char and uint8 same?

Answer

greydet picture greydet · Feb 8, 2016

Both types are 8bits long. The difference comes with signedness.

  • The uint8 type is unsigned.
  • The char type should be signed in your case. Actually, it is compiler dependent, but most compilers consider the char type as signed by default and have an option to force char type as unsigned if needed. See the C99 standard document reference §6.2.5p15:

The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

CHAR_MIN, defined in limits.h, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options.