atrcmp and ==

M

Meenu

Hi,
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
 
C

canbaby

strcmp is certainly!
but == is used to compare with the two str's address
you cannot use == to compare string, no more try it.
 
J

John Bode

Meenu said:
Hi,
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

You can't compare array *contents* using the "==" operator. When you
write

str1 == str2

what you are comparing are the *addresses* of the two arrays, not their
contents. Remember that in most contexts (including this one), an
array identifier is converted to a pointer to the base type, and its
value is converted to the base address of the array.
 
E

Emmanuel Delahaye

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 pointers values while the strcmp()
function compares the string char by char. Big difference.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

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.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
B

Ben Pfaff

Meenu said:
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

This is a FAQ.

8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?

char *string;
...
if(string == "value") {
/* string matches "value" */
...
}

A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.

To compare two strings, you generally use the library function
strcmp():

if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}
 
C

CBFalconer

Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.
 
C

Christian Kandeler

CBFalconer said:
Emmanuel said:
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.


Christian
 
F

Flash Gordon

CBFalconer said:
Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.

Sorry, but you miss-read the example code. The str1 and str2 are char
arrays initialised using identical string literals, not pointers to the
identical string literals.
 
G

Giannis Papadopoulos

CBFalconer said:
Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.

For the compiler to do the relevant optimization, shouldn't they be
declared as

char *str1 = "Hello";
char *str2 = "Hello";

?

I am allowed to do

char str1[] = "Hello";
str1[0] = 'G';

whereas if I'd try

char *a = "Hello";
a[0] = 'G';

I get a segmentation fault..


--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 
C

CBFalconer

Christian said:
CBFalconer said:
Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.
 
E

Emmanuel Delahaye

CBFalconer wrote on 02/08/05 :
Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.

Nice try, but I don't see any string constant here. They are
initialized arrays of char, and they are mutable.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
C

CBFalconer

Giannis said:
CBFalconer said:
Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.

For the compiler to do the relevant optimization, shouldn't they be
declared as

char *str1 = "Hello";
char *str2 = "Hello";

You are absolutely right, and I have been talking through my
oversized floppy hat.
 
A

akarl

CBFalconer said:
Emmanuel said:
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. The system is allowed to merge
identical string constants. The result of those test would tell
whether is has done so. This is one fundamental reason for those
constants not to be writeable.

I can't see how str1 could be equal to str2. If that was the case then
e.g. str1[0] = 'F' would make str2[0] == 'F'. The declarations above
declares two arrays of length six, initialized with the string "Hello".
On the other hand if the declarations would be

char *str1 = "Hello";
char *str2 = "Hello";

then I'm with you.


August
 
A

Andrey Tarasevich

CBFalconer said:
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. The system is allowed to merge
identical string constants. 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 "system " is indeed allowed to merge identical string literals. But that's
all that it is allowed to do: merge the anonymous string literal objects. This
means that the first "Hello" literal might have that same address as the second
"Hello" literal. However, this has absolutely noting to do with objects 'str1'
and 'str2'. These two are distinct array objects. They are not string literals.
They are distinct from each other, they are distinct form the anonymous string
literal objects. The implementation is not allowed to "merge" 'str1' and 'str2'.
'str1' and 'str2' are always guaranteed to have different addresses in storage,
meaning that the above '==' operator is guaranteed to evaluate to 0.

A different example might look as follows

const char* str1 = "Hello";
const char* str2 = "Hello";

In this case the pointers are initialized with the addresses of the actual
anonymous string literal objects. And in this case, if the literals get merged,
the pointers will indeed compare equal. But not in the original example.
 
P

pete

CBFalconer said:
Christian said:
CBFalconer said:
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.
 
C

CBFalconer

pete said:
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.

I goofed. I fouled up. I was wrong. I didn't read the
declarations. The cat was climbing my leg. My evil twin made me
do it. It's all Bushs fault. How long is it going to take to live
this down?
 
J

Joe Wright

Meenu said:
Hi,
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
Hi yourself.

Get a C book and read it. Write some C programs. Read the C book again,
and some more. Write more programs.

This is not (I hope) a chat room. Any of several good C books will
explain == and strcmp() and lots of stuff. Read first!

Don't be afraid. The answers are always in the back of the book. Well,
at least sometimes. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top