A little help with some C++??
Thric
Posts: 109
So for one of my propeller projects i've decided to go a little fancy and learn some C++ to program my computer to later communicate through RS-232 with my propeller.
I've just started diving into this language and i'm stuck on converting one variable type to another. when i try to convert an unsigned char * to a char type by:
unsigned char *temp;
char buff;
buff=(char)temp;
I get an error that says:
cast from 'unsigned char*' to 'char' loses precision
not sure what i'm missing here. Any help would be great!
Thanks
I've just started diving into this language and i'm stuck on converting one variable type to another. when i try to convert an unsigned char * to a char type by:
unsigned char *temp;
char buff;
buff=(char)temp;
I get an error that says:
cast from 'unsigned char*' to 'char' loses precision
not sure what i'm missing here. Any help would be great!
Thanks
Comments
An unsigned byte consists of an 8-bit storage unit that holds a value from 0 to 255. A signed byte consists of an 8-bit storage unit that holds a value from -128 to 127. If you have an unsigned byte (unsigned char) assigned to signed byte (char) or vice versa, some of the values won't convert. That's what the warning message means. The C++ compiler should still generate code for you and the program should run unless you've told it not to allow warnings. If temp happens to hold a value from 128 to 255, it will seem to have a value from -128 to -1 when it's assigned to buff. That may not be what you want.
In the above 'temp' is a pointer, an address which points to an unsigned char. On a 32-bit system this would be a 32-bit value. However, 'buff' is a character, an 8-bit value. So you can't copy 'temp' into 'buff', just like you don't want to copy an index into a value.
There are two possibilities here as to what was meant: or In both cases some compilers may give a warning, that depends on the compiler. NB: I wrote the above assuming that a C++ compiler will handle that code as C, as it's written as C and not C++.
-Tor
Thanks!
You can always have it run the last successfully compiled version, but as long as there are compiler errors then you won't be able to run the current version. Note that if you have the -werror compiler option on then your warnings will be considered errors (not on by default, IIRC, but it may happen).