Shop OBEX P1 Docs P2 Docs Learn Events
A little help with some C++?? — Parallax Forums

A little help with some C++??

ThricThric Posts: 109
edited 2011-10-23 13:28 in General Discussion
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-10-23 12:04
    This is an issue that shows up in Propeller and Stamp programming as well although the compilers for these don't check for it. It also occurs in C.

    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.
  • ThricThric Posts: 109
    edited 2011-10-23 12:51
    Hmm i see the issue. I'm not sure if it will effect my program, so i'll try to see if it works and if it does i'll leave it at that. You said that it should run but Code block prevents me from running so is there a way to disable this? Thanks for the help.
  • TorTor Posts: 2,010
    edited 2011-10-23 12:54
    Actually in this case it's worse than just signed/unsigned.
    unsigned char *temp;
    char buff;
    buff=(char)temp;
    
    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:
    /* Copy the character that 'temp' points to into 'buff':
    unsigned char *temp;
    char buff = (char)*temp; /* Note the '*' to get the value of what 'temp' points to */ 
    
    or
    /* Copy the 'temp' pointer: */
    unsigned char *temp;
    char *buff = (char *)temp; /* Copy one pointer to another, but cast it to another pointer type */
    
    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
  • ThricThric Posts: 109
    edited 2011-10-23 13:05
    Tor thanks for the help! that clears somethings up. And now code blocks doesn't seem to hate me anymore!
    Thanks!
  • SRLMSRLM Posts: 5,045
    edited 2011-10-23 13:28
    Thric wrote: »
    ...You said that it should run but Code block prevents me from running so is there a way to disable this? Thanks for the help.

    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).
Sign In or Register to comment.