CBFalconer said:
Christian said:
CBFalconer wrote:
Emmanuel Delahaye wrote:
Meenu wrote on 02/08/05 :
Why is there a difference when we compare
char str1[]="Hello";
char str2[]="Hello";
with strcmp and ==
strcmp gives them as equal and == gives them as unequal
The == operator compares the arrays addresses
(obvioulsy different,
because str1 and str2 are different objects) while the strcmp()
function compares the string char by char. Big difference.
They may not be different objects.
They definitely are in this case. Note that the OP did not compare
the string literals, but named objects.
You are wrong. Why did you snip my explanation of why this is so,
which I have appended below.
They may not be different objects.
They are different objects.
The system is allowed to merge
identical string constants.
That doesn't matter.
The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.
The two different objects are str1 and str2.
It doesn't matter if they are initialised
with the same string literal.
String literals are not converted to pointers
in the case of array initialization.
char str1[]="Hello";
means the exact same thing as
char str1[]= {'H','e','l','l','o','\0'};
Addresses aren't part of the semantics of that
code with the string literal.