|
|
View previous topic :: View next topic |
Author |
Message |
mike holeton Guest
|
how do I use the return of (-1) |
Posted: Tue May 10, 2005 9:39 pm |
|
|
example code
char CompareName( char *OtherName )
{
char i;
for (i = 0; (i<J1939_DATA_LENGTH) && (OtherName[i] == CA_Name[i]); i++);
if (i == J1939_DATA_LENGTH)
return 0;
else if (CA_Name[i] < OtherName[i] )
return -1; //how do I do this?
else
return 1; |
|
|
Ttelmah Guest
|
|
Posted: Wed May 11, 2005 3:13 am |
|
|
Key thing. A value has to be 'signed', to return a negative number. As written, a program at the other 'end', will 'see' 255'. A 'char' (and an integer), in CCS C, default to unsigned data types.
Basically, '-1', is coded in binary, as '11111111'. A character is an unsigned value, so the subroutine will return 255 (which is what the same binary value gives when treated as an unsigned number).
If you want to return a -1 'error' status, you have to change the declaration of the function, to:
signed char CompareName( char *OtherName )
Remember though that with a 'signed' declaration, the maximum value that can be returned, drops to 127.
Best Wishes |
|
|
Paolino
Joined: 19 Jan 2004 Posts: 42
|
|
Posted: Wed May 11, 2005 3:14 am |
|
|
I have understood this: the statment
has no effect in your code. Is it correct? If so, you have to declare differently the function and the variable returned. Infact, directly from CCS help on line
Quote: |
All types, except float, by default are unsigned; however, maybe preceded by unsigned or signed
|
Try this:
Code: |
signed char CompareName( char *OtherName )
{
char i;
for (i = 0; (i<J1939_DATA_LENGTH) && (OtherName[i] == CA_Name[i]); i++);
if (i == J1939_DATA_LENGTH)
return 0;
else if (CA_Name[i] < OtherName[i] )
return -1; //Now it should work
else
return 1;
}
|
If you prefer to keep the funciotn as is, you have to return a positive value, instead of -1. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|