Understanding Char Array equality in C

后端 未结 5 868
忘了有多久
忘了有多久 2021-01-19 13:47

Sorry in advance for the ignorance. I don\'t fully understand how to compare char arrays in C. I was originally comparing two char arrays in c with the simple ==

相关标签:
5条回答
  • 2021-01-19 14:36

    Doing this if (a == b) will compare Pointer values stored in a and b.

    So if you have a

      a //say at some random address 1000 
    

    or b

      b //say at some random address 2000 
    

    Is a==b? Now normally if the compiler is doing string pooling and if your string literals are exactly same then this may work in those cases - otherwise you have to do a character-by-character comparison as strcmp would i guess.

    0 讨论(0)
  • 2021-01-19 14:48

    String Literals are stored in contiguous memory locations in Text(Read Only) segment of the memory.

    char *a = "test";
    char *b = "test";
    if (a == b) ..do something
    

    Here you are comparing the address of the first elements of the arrays. This can results in equality as "test" being an String stored in text segment of the memory and *a and *b might point to that memory location.

    char *a = "test";
    char *b = "test";
    if (0 == strcmp(a, b)) ..do something
    

    Here you are comparing each element of both arrays byte by byte till NULLCHAR(\0) for any one of the input array is reached.

    0 讨论(0)
  • 2021-01-19 14:50
    if (a == b)
    

    Here you are comparing pointers and not the strings

    strcmp(a, b)
    

    Here you are comparing strings

    Which one is correct and why? What is the other one doing?
    

    Since there are 2 strings stored in different memory locations or if the same string is being stored there is possibility a==b comparing pointers doesn't make sense.What you want is to compare the contents of the locations the pointers are pointing to. Which is done by strcmp() and this is the right way to compare the strings.

    For example :

    #include <stdio.h>
    
    int main(void) {
    char *a = "test";
    char *b = "test";
    
    printf("%p\n %p",(void *)a,(void *)b);
        return 0;
    }
    

    The output is

    0x8048540 
    0x8048540
    

    So both the pointers a and b are pointing to the same memory location a==b Note that here what we compare is not the actual characters in the string but just the pointers.

    I ran the same code on another machine and the locations in which this string was stored was different.

    0x4005f8
    0x4005fd
    

    So now even though the strings might be same you see a != b. Hence use strcmp() to compare strings.

    0 讨论(0)
  • 2021-01-19 14:50

    I would recommend to use strcmp because it compares the contents of the strings while == compares the address of the first elements in the strings.

    Also, strcmp will tell you the relative ordering of the strings rather than simply if they are equal.

    0 讨论(0)
  • 2021-01-19 14:51

    if(a == b) will compare the addresses stored in a and b pointers.

    strcmp(a, b) will compare character by character of the contents stored at a and b addresses. It returns 0 if both contents are same (case sensitive). otherwise difference in ASCII values of the characters

    if(*a == *b) will compare the first characters (i.e. char at 0th position) of both array.

    Hope it helps !!

    0 讨论(0)
提交回复
热议问题