471,579 Members | 1,533 Online

# difference between memcmp and strcmp....

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 )
Oct 9 '06 #1
4 13831
Banfa
9,065 Expert Mod 8TB
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.
Oct 9 '06 #2
ok i see...
what can you suggest for me to use?
or is it ok to compare using the logical operators in comparing?
Oct 10 '06 #3
sorry sir i am not understanding this mem strcom diffrence
Oct 10 '06 #4
D_C
293 100+
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.
Oct 10 '06 #5