Note: expected ‘char * __restrict__’ but argument is of type ‘int8_t *’

cgcc

code:

int8_t ret;
int8_t buf[8];
bytes_written = snprintf(buf, 8, "%" PRId8, 2);

warning:

warning: pointer targets in passing argument 1 of ‘snprintf’ differ in signedness [-Wpointer-sign]
     bytes_written = snprintf(buf, 8, "%" PRId8, 2);
     ^
/usr/include/stdio.h:386:12: note: expected ‘char * __restrict__’ but argument is of type ‘int8_t *’
 extern int snprintf (char *__restrict __s, size_t __maxlen,

I know this can be fixed by taking buf as *char, but

int8_t is typedef as unsigned char

checked with pre-processor output i.e. gcc main.c | grep int8_t

So why compiler is not able to understand that?

With buf as uint8_t also I get the same warning.

Edit:

int8_t is typedef as signed char (By mistake I have written as unsigned in original post)

Best Answer

The sprintf family of calls require a char * buffer in which to write their data.

You currently have it as int8_t * (a signed 8-bit value) and the fact that it's complaining about the signedness of the type almost certainly means the naked char is unsigned on your system (the standard leaves it open as to whether or not char is a signed or unsigned type) or that gcc is clever enough to realise this may be a portability issue on other compilers.

Be aware that's a warning so it will probably still work. However, I like to clean up code like that so as to make my life easier down the track.

The fix is, of course, to use the correct type (either by changing the type to char buf[8] or by explicitly casting it in the snprintf call, thereby telling the compiler you know what you're doing).

Related Topic