• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

char V uint8_t V int8_t

Status
Not open for further replies.

Pommie

Well-Known Member
Most Helpful Member
I keep getting conversion errors when using int8_t instead of char. This only happens on esp8266 boards and I'd like to figure out why.

Here's an example,
Code:
char buff[128];       //can't change this to uint8_t or int8_t

void setup() {
  strcpy(buff,"Hello World!");
}
This compiles fine but complains if I change to uint8_t, int8_t, or byte but only on esp8266. It always works fine on an Uno.
If I add a cast to char (in strcpy) then I get a loss of precision error! How can going from 8 bits to a char loose precision?
I thought char and int8_t were interchangeable!!

I'm guessing that esp is using something other than 8 bits for a char.
Any thoughts anyone?

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
Did you include types.h?
If I include types.h it isn't found. stdint.h is found but makes no difference.

With uint8_t I get the following error,
invalid conversion from 'uint8_t* {aka unsigned char*}' to 'char*' [-fpermissive]

With int8_t I get the following error,
invalid conversion from 'int8_t* {aka signed char*}' to 'char*' [-fpermissive]

How is signed char* different to char*? It's as though there is a third kind of char.

If I include a cast, strcpy((char)buff,"Hello World!");
The error is "
error: cast from 'uint8_t* {aka unsigned char*}' to 'char' loses precision [-fpermissive]
strcpy((char)buff,"Hello World!");

I just don't understand what's going on.

I can work around it but would prefer to understand it.

Mike.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I hate all these conventions... I only ever use char or unsigned char..BUT!!! some compilers treat char as unsigned unless signed is specifically used...( In compiler settings I check "treat char as signed") but that is for the pic stuff..

If you need to check out "core" specific details ( I have a chipkit pic32 core) they are stored in users\"user(you)"\appdata\local\arduino"x".. Here be all the relevant files...
 

Cicero

Active Member
If I include types.h it isn't found. stdint.h is found but makes no difference.

With uint8_t I get the following error,
invalid conversion from 'uint8_t* {aka unsigned char*}' to 'char*' [-fpermissive]

With int8_t I get the following error,
invalid conversion from 'int8_t* {aka signed char*}' to 'char*' [-fpermissive]

How is signed char* different to char*? It's as though there is a third kind of char.

If I include a cast, strcpy((char)buff,"Hello World!");
The error is "
error: cast from 'uint8_t* {aka unsigned char*}' to 'char' loses precision [-fpermissive]
strcpy((char)buff,"Hello World!");

I just don't understand what's going on.

I can work around it but would prefer to understand it.

Mike.
You need to cast to a char pointer, ie: strcpy((char*)buff,"Hello World!");

Then you should be ok.

To add to this, they're just considered different types, its annoying and confusing for sure. But its giving you errors because they're essentially different types, doesn't matter that they can be cast without having any issues. https://stackoverflow.com/questions/436513/char-signed-char-char-unsigned-char
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
Yup, that fixes it, thank you and I should have realized the cast was wrong. However, why doesn't it work without the cast? How is uint8_t or int8_t different from char? I also tries uint_least8_t as I was sure that was 8 bit and it still errored.

Mike.
Edit, read your link and it seems there are 3 types of char. Bizarre.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top