Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int8_t and char: converts between pointers to integer types with different sign - but it doesn't

Tags:

c++

c

char

embedded

I'm working with some embedded code and I am writing something new from scratch so I am preferring to stick with the uint8_t, int8_t and so on types.

However, when porting a function:

void functionName(char *data)

to:

void functionName(int8_t *data)

I get the compiler warning "converts between pointers to integer types with different sign" when passing a literal string to the function. ( i.e. when calling functionName("put this text in"); ).

Now, I understand why this happens and these lines are only debug however I wonder what people feel is the most appropriate way of handling this, short of typecasting every literal string. I don't feel that blanket typecasting in any safer in practice than using potentially ambiguous types like "char".

like image 369
AndrewN Avatar asked Mar 19 '23 10:03

AndrewN


1 Answers

You seem to be doing the wrong thing, here.

Characters are not defined by C as being 8-bit integers, so why would you ever choose to use int8_t or uint8_t to represent character data, unless you are working with UTF-8?

For C's string literals, their type is pointer to char, and that's not at all guaranteed to be 8-bit.

Also it's not defined if it's signed or unsigned, so just use const char * for string literals.

like image 108
unwind Avatar answered Mar 22 '23 01:03

unwind