Thread: Character Array Comparison

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    1

    Character Array Comparison

    Code:
    char a[10] = "123";
    char b[10] = "234";
    if (a > b) {
         printf("here");
    }
    My question is, what is that comparison doing. Is it comparing the memory address of those two variables? Or what?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes it is comparing the memory address of those two variables. (The value of "a", by itself, is not a member of the array a but the beginning address of the array. Same for "b".)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can loop through and compare each character, or you can ... do the same exact thing, disguised as a single function call, using something like strcmp. Assuming of course that you do have strings and not just non-nul-terminated-arrays.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  2. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM