By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,493 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,493 IT Pros & Developers. It's quick & easy.

difference between memcmp and strcmp....

P: 25
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 )


if(memcmp(aAbsCylNumHigh, bAbsCylNumHigh,2)<=0 && memcmp(aAbsCylNumLow,bAbsCylNumLow,2)<=0 && memcmp(aSecNum,.bSecNum,2)<0 )
Oct 9 '06 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 5K+
P: 8,916
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

P: 25
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

P: 3
sorry sir i am not understanding this mem strcom diffrence
please give me a example
Oct 10 '06 #4

P: 293
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

Post your reply

Sign in to post your reply or Sign up for a free account.