471,579 Members | 1,533 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 software developers and data experts.

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
please give me a example
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

Post your reply

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

Similar topics

16 posts views Thread by rajkumar | last post: by
4 posts views Thread by blueblueblue2005 | last post: by
6 posts views Thread by Sidney Cadot | last post: by
7 posts views Thread by Alex Stark | last post: by
3 posts views Thread by Sonnich | last post: by
3 posts views Thread by gevadas | last post: by
39 posts views Thread by Martin | last post: by
1 post views Thread by jawaraj1911 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.