CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how do I use the return of (-1)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
mike holeton
Guest







how do I use the return of (-1)
PostPosted: Tue May 10, 2005 9:39 pm     Reply with quote

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







PostPosted: Wed May 11, 2005 3:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 11, 2005 3:14 am     Reply with quote

I have understood this: the statment
Code:

return -1;

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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