Reputation: 333
I read many time the ==
operator doesn't works on C strings.
But I'm confused by this sample, tested thanks to Coliru website.
#include <stdio.h>
typedef struct S {
const char *data;
} S;
const char *Text = "hello";
void test()
{
n = { "hello" };
printf("%d\n", n.data == Text);
}
int main(void)
{
test();
return 0;
}
If you change any letter of a value, (Text
or n
), the result is 0
and if it is exactly the same the result is 1
.
If ==
is unsuitable for strings, in which case this operator will give unexpected results ?
Upvotes: 1
Views: 113
Reputation: 145297
The ==
operator, as well as the relations comparison operators <
, <=
, >
and >=
, when applied to pointer types, compares the pointer values, not the objects pointed to.
Hence a == b
will evaluate to 1
if and only if a
and b
point to the same object.
In your example, Text
and n.data
are initialized with two identical string literals "hello"
. It is implementation defined whether these string literals are compiled as 2 different objects or as a single object.
On your system, the compiler generates the same object, so both pointers point to the same object and thus are identical.
Try this code to verify that ==
is inappropriate to compare strings:
#include <stdio.h>
#include <string.h>
typedef struct S {
const char *data;
} S;
int main(void) {
char copy[] = "hello";
const char *Text = "hello";
S n = { "hello" };
printf("pointer comparison: %d, string comparison: %d\n",
n.data == Text, strcmp(n.data, Text) == 0);
printf("pointer comparison: %d, string comparison: %d\n",
n.data == copy, strcmp(n.data, copy) == 0);
return 0;
}
Output:
pointer comparison: 1, string comparison: 1
pointer comparison: 0, string comparison: 1
Upvotes: 2
Reputation: 311126
The result of this call
printf("%d\n", n.data == Text);
is implementation defined. This call can output either 1
or 0
. There are compared two pointers (their values) to identical string literals (to first characters of identical literals). The result depends on compiler options that is whether the compiler stores identical string literals as distinct arrays or as one array (with static storage duration).
From the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
To compare strings you should use the standard C string function strcmp
.
Upvotes: 1