Comparing char arrays

This is a discussion on Comparing char arrays within the C Programming forums, part of the General Programming Boards category; Hi was wondering if I am comparing 2 char arrays of the same size, can i use memcmp? Or is ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Comparing char arrays

    Hi was wondering if I am comparing 2 char arrays of the same size, can i use memcmp?

    Or is there a better way of comparing?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If that's the type of comparison you need, it's fine. Did you try it? Is that the type of comparison you need?

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Quote Originally Posted by Edelweiss View Post
    Hi was wondering if I am comparing 2 char arrays of the same size, can i use memcmp?

    Or is there a better way of comparing?
    It depends. strcmp is a lexicographical comparison which means the function understands 'a' is less than 'b' because 'a' comes before 'b' and so forth. memcmp is not.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by whiteflags View Post
    It depends. strcmp is...
    ...for strings.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    lol. but if i want to use memcmp between an uint8_t array of size 2 and a uint16_t variable, does it still work? since its comparing the memory at the address right?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union moarcheating
    {
        uint8_t a[2];
        uint16_t b;
    } a, b;
    
    a.a[0] = 0xAA;
    a.a[1] = 0xBB;
    
    b.b = 0xAABB; /* assuming your endian is correct */
    
    if( b.b == a.b )
        printf( "omg hax\n" );

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    But if your uint16 contains 0x1234 and your uint8 array contains [ 0x12, 0x34 ] then it is down to the endian of your machine as to whether they compare equal or not (using memcmp)

    You're better off with
    if ( (var & 0xff) == array[0] && ((var>>8)&0xff) == array[1] )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing two arrays
    By Gonz in forum C Programming
    Replies: 2
    Last Post: 03-23-2011, 12:38 AM
  2. Comparing 2 arrays
    By shel5210 in forum C Programming
    Replies: 2
    Last Post: 03-30-2010, 01:03 AM
  3. Comparing 2 char arrays
    By Rasher in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2010, 12:48 PM
  4. Comparing Arrays
    By cjohnman in forum C Programming
    Replies: 6
    Last Post: 04-24-2008, 03:24 PM
  5. comparing arrays
    By dustyrain84 in forum C Programming
    Replies: 1
    Last Post: 01-21-2004, 04:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21