Connecting Tech Pros Worldwide Forums | Help | Site Map

difference between memcmp and strcmp....

Newbie
 
Join Date: Aug 2006
Location: Bacoor, Cavite, Philippines
Posts: 25
#1: Oct 9 '06
which of the 2 commands are applicable in comparing an array of unsigned chars?

if(strcmp(aAbsCylNumHigh, bAbsCylNumHigh)<=0 && strcmp(aAbsCylNumLow,bAbsCylNumLow)<=0 && strcmp(aSecNum,.bSecNum)<0 )

or

if(memcmp(aAbsCylNumHigh, bAbsCylNumHigh,2)<=0 && memcmp(aAbsCylNumLow,bAbsCylNumLow,2)<=0 && memcmp(aSecNum,.bSecNum,2)<0 )

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: Oct 9 '06

re: difference between memcmp and strcmp....


The difference is in the third parameter (not there in strcmp).

Basically strcmp assumes you have passed pointers to zero terminated strings and so the compare stops when it finds the first terminator '\0' in either string.

memcmp makes no such assumption so you have to pass it the number of bytes to compare.

In English

strcmp compares 2 arrays of char that contain zero terminated strings

memcmp compares 2 arrays of char that contain any data.
Newbie
 
Join Date: Aug 2006
Location: Bacoor, Cavite, Philippines
Posts: 25
#3: Oct 10 '06

re: difference between memcmp and strcmp....


ok i see...
what can you suggest for me to use?
or is it ok to compare using the logical operators in comparing?
Newbie
 
Join Date: Oct 2006
Posts: 3
#4: Oct 10 '06

re: difference between memcmp and strcmp....


sorry sir i am not understanding this mem strcom diffrence
please give me a example
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#5: Oct 10 '06

re: difference between memcmp and strcmp....


Suppose a and b are pointers such that:
*a = 0x68002573
*b = 0x68003673

strcmp(a,b) returns equal because "D" equals "D".
memcmp(a,b,4) returns b > a, since the first and second bytes are the same, but the third byte of b is greater than the third byte of a. The rest of the bytes (just the 4th one) is unnecessary. If memcmp compares the four bytes from both strings, and they are the same, it will return b = a.

With strcmp, it goes until it finds a byte with value 0x00 in either string. You need to pass to memcmp a number of how many bytes to compare. It will compare that many bytes unless there is a difference between respective byte values.
Reply


Similar C / C++ bytes