user1157977
user1157977

Reputation: 917

char * compare with std string wrong result

case 1:

std::string dd = "5A"
char checkSum[9]   
checkSum[0] = '5';  
checkSum[1] = 'A';  
if(strcmp(dd.c_str(),checkSum) == 1){ 
return 1;
} 
else {return 0;}

RESULT: returns 1 //Correct!

case 2:

std::string dd = "0A"
char checkSum[9];  
checkSum[0] = '5';  
checkSum[1] = 'A';  
if(strcmp(dd.c_str(),checkSum) == 1){  
return 1;
} 
else {return 0;}

RESULT: returns 0 //Correct!

case 3:

std::string dd = "5A"    
char checkSum[9]    
checkSum[0] = '0'; 
checkSum[1] = 'A'; 
if(strcmp(dd.c_str(),checkSum) == 1){ 
return 1;
} 
else {return 0;}

RESULT: returns 1 //Not Correct!

Anyone knows whats wrong? thx!

Upvotes: 0

Views: 1583

Answers (4)

David Heffernan
David Heffernan

Reputation: 613262

strcmp requires two null-terminated strings, but you are not adding a null-terminator to checkSum.

This is why case 1 returns 1 when in fact it should return 0. Note that your expected values are incorrect. In case 1, once you have added in the null-terminator, the two strings should compare as equal. And so strcmp will return 0 and your code should also return 0.

For case 2, strcmp("0A", "5A") returns a negative value since '0'<'5' and so your code should return 0.

For case 3, strcmp("5A", "0A") returns a positive value, and it just so happens that the positive value is 1 which you are mistakenly testing for by equality.

In all cases testing for a value of 1 is incorrect since strcmp never promises to return 1. It promises to return either:

  • a value of 0 to indicate that the two strings are equal, or,
  • a positive value to indicate that str1 compares greater than str2, or,
  • a negative value to indicate that str1 compares less than str2.

The only valid comparisons on the return value of strcmp are therefore ==0, >0 or <0.

You need to revisit the documentation for strcmp and correct your understanding of how to interpret the return value.

Upvotes: 7

A.H.
A.H.

Reputation: 66273

Also you should never compare the result of strcmp with anything else then zero as in

strcmp(s1, s2) == 0
strcmp(s1, s2) > 0    
strcmp(s1, s2) < 0
strcmp(s1, s2) != 0

as the standard does not guerantee a 1 (or -1) in the case of difference.

Upvotes: 0

Antonio P&#233;rez
Antonio P&#233;rez

Reputation: 6992

strcmp return value:

A zero value indicates that both strings are equal. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

(Extract from http://www.cplusplus.com/reference/clibrary/cstring/strcmp/)

Upvotes: 0

AlexTheo
AlexTheo

Reputation: 4184

Your checksum contain undefined data you don't add a '\0' termination character in it. It is unexpected behavior if you call the strcmp. You could declare your checksum as: char checkSum[9] = {0}; but better solution is to avoid unsafe character arrays, use std::string instead.

Upvotes: 1

Related Questions