difference between memcmp and strcmp....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • system55
    New Member
    • Aug 2006
    • 25

    difference between memcmp and strcmp....

    which of the 2 commands are applicable in comparing an array of unsigned chars?

    if(strcmp(aAbsC ylNumHigh, bAbsCylNumHigh) <=0 && strcmp(aAbsCylN umLow,bAbsCylNu mLow)<=0 && strcmp(aSecNum, .bSecNum)<0 )

    or

    if(memcmp(aAbsC ylNumHigh, bAbsCylNumHigh, 2)<=0 && memcmp(aAbsCylN umLow,bAbsCylNu mLow,2)<=0 && memcmp(aSecNum, .bSecNum,2)<0 )
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • system55
      New Member
      • Aug 2006
      • 25

      #3
      ok i see...
      what can you suggest for me to use?
      or is it ok to compare using the logical operators in comparing?

      Comment

      • shaikh riyaz
        New Member
        • Oct 2006
        • 3

        #4
        sorry sir i am not understanding this mem strcom diffrence
        please give me a example

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          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.

          Comment

          Working...