1

Here in my sample code, I am comparing two char pointer and two char array and output is

Output:
     case I :true
     case II : false

for case II, I understood that it's evaluated false because memory allocation of arr1 and arr2 is different. But I am not getting why case I evaluated as true.

int main()
    {
    char *string = {"string"};
    char *string2 = {"string"};

    char arr1[] = {"string"};
    char arr2[] = {"string"};

    /******* case I **********/
    if(string == string2){
      printf("case I :true \n");
     }
    else{
      printf("case I :false \n");
    } 

    /****** case II **********/
    if(arr1 == arr2){
      printf("case II : true \n");
     }
    else{
      printf("case II : false \n");
    }
   return 0;
   }
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43
  • 4
    First, you're not comparing strings, you're comparing pointers. Second, the first pair are pointers to constant literals, and your compiler is *likely* folding them into a single constant string in your read-only data segment, therefore *both* `string` and `string2` contain the same *address*. – WhozCraig Dec 01 '13 at 08:23
  • 2
    Now I have to ask, the `char *string = {"string"};`, what does it mean, why does it compile, and does it compile without warnings? – hyde Dec 01 '13 at 08:26
  • @hyde it's compiling without any warning. – subhash kumar singh Dec 01 '13 at 08:28
  • @Mat you are correct if I compile the code with -fwritable-strings then case I gives false. Now I understood that case I is true because of string pooling. thanks. – subhash kumar singh Dec 01 '13 at 08:54
  • 1
    @hyde: Using braces for a scalar (such as a pointer) is permitted by C 2011 (N1570) 6.7.9: “The initializer for a scalar shall be a single expression, optionally enclosed in braces.” I do not know why this is permitted but would hypothesize either it is historical (e.g., to accommodate compilers that permitted it before the language was standardized) or there is some case where automatic code generation is easier if braces are always used for the initializer, regardless of whether the object being initialized is a scalar or an aggregate. – Eric Postpischil Dec 01 '13 at 10:31

4 Answers4

1
if(string == string2){  

is comparing pointers not string. In first get it becomes true because both string and string2 points to the same memory location and condition becomes true.
For string compare use library function strcmp. You will get right result.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

arr1 and arr2 these two are pointers, so when you compair these two those pointers will be compaired which are never same. Thats why you get output false. Now about the first case:

When you write:

char *string = {"string"};
char *string2 = {"string"};

In memory compiler allocates space for your character sequence and stores a pointer to this area in string and string2. As a result both of this two are immutable.

If you do:

*(string)= 'a';

You'd get error.

String literals in C have static storage duration. They are never allocated "on the stack". They are always allocated in global/static memory, and remains as long as the program runs.

This is called String interning i.e. a method of storing only one copy of each distinct string value (as wikipedia says) and is an example of Flyweight Design Pattern

More detail is available here

Community
  • 1
  • 1
deeiip
  • 3,319
  • 2
  • 22
  • 33
0

arr1 == arr2 means that you are comparing address of first element of arr1 and arr2 . so that why it giving false .

char *string = {"string"}; "string" is constant and both string and string1 points to same location thats why string == string2 giving true

you should use strcmp to compare char's string . operator == works with string in c++

EmptyData
  • 2,386
  • 2
  • 26
  • 42
0
char *string = {"string"};
char *string2 = {"string"};

"string" in both *string and *string2 are string literals, same string literals share same memory space. That's why (string == string2) turns out to be true.

Yuankun
  • 6,875
  • 3
  • 32
  • 34