473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

atrcmp and ==

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

Nov 15 '05 #1
27 1405
strcmp is certainly!
but == is used to compare with the two str's address
you cannot use == to compare string, no more try it.

Nov 15 '05 #2

Meenu wrote:
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.

Nov 15 '05 #3
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++
Nov 15 '05 #4
(supersedes <mn***********************@YOURBRAnoos.fr>)

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."
Nov 15 '05 #5
"Meenu" <me******@yahoo.com> writes:
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" */
...
}
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 15 '05 #6
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. 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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 15 '05 #7
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.
Christian
Nov 15 '05 #8
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. 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.
Nov 15 '05 #9
Christian Kandeler wrote:
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. 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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #10
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. 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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #11
CBFalconer wrote on 02/08/05 :
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. 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++
Nov 15 '05 #12
Giannis Papadopoulos wrote:
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. 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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 15 '05 #13
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. 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
Nov 15 '05 #14
CBFalconer wrote:

> 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.

--
Best regards,
Andrey Tarasevich
Nov 15 '05 #15
CBFalconer wrote:

Christian Kandeler wrote:
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.

--
pete
Nov 15 '05 #16
pete <pf*****@mindspring.com> writes:
CBFalconer wrote:

[...]
They may not be different objects.


They are different objects.


Yes, and he's already acknowledged his error.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #17
Keith Thompson wrote:
pete <pf*****@mindspring.com> writes:
CBFalconer wrote:

[...]
They may not be different objects.


They are different objects.


Yes, and he's already acknowledged his error.


Can I kick him ribs? Just one time?


Brian
Nov 15 '05 #18
pete wrote:
CBFalconer wrote:
Christian Kandeler wrote:
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?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #19
Meenu wrote:
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. :-)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #20
Andrey Tarasevich wrote:
CBFalconer wrote:
>>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.

--
Best regards,
Andrey Tarasevich


I think you should first read, then respond...

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

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 15 '05 #21
On Wed, 03 Aug 2005 01:09:59 GMT, CBFalconer
<cb********@yahoo.com> wrote:
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?


Since it's less than a day since you goofed, and much less than a dasy
since you first admitted it, you can probably expect several more.
Usenet is not an 'instant' messaging system, many people only read it
once a day or less (and may not upload their replies until the next
session).

It's a bit like programs where one missing semicolon causes hundreds of
errors, getting weirder as the compiler gets further through the
program. Eventually they stop, but not before many sheets of
lineprinter paper have been used...

Chris C
Nov 15 '05 #22
Chris Croughton wrote
(in article <sl******************@ccserver.keris.net>):
It's a bit like programs where one missing semicolon causes hundreds of
errors, getting weirder as the compiler gets further through the
program. Eventually they stop, but not before many sheets of
lineprinter paper have been used...


This is reminding me of how old this crowd is (including
myself). I suspect that c.l.c demographics are much older than
most Usenet groups today.

I know what you're talking about, but the last time I used a
lineprinter, or had compilation output go to one was 1982, and
that was Fortran. :-)

--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #23
Giannis Papadopoulos wrote:
...
I think you should first read, then respond...


Huh? I think you should either post something meaningful or not post at
all. What exactly were you trying to say?

--
Best regards,
Andrey Tarasevich
Nov 15 '05 #24
On Wed, 03 Aug 2005 17:38:40 GMT, Randy Howard
<ra*********@FOOverizonBAR.net> wrote:
Chris Croughton wrote
(in article <sl******************@ccserver.keris.net>):
It's a bit like programs where one missing semicolon causes hundreds of
errors, getting weirder as the compiler gets further through the
program. Eventually they stop, but not before many sheets of
lineprinter paper have been used...
This is reminding me of how old this crowd is (including
myself). I suspect that c.l.c demographics are much older than
most Usenet groups today.


I don't know, it seems that most groups I've seen fall prey to the
Yorkshiremen sketch ("When I were a lad...") occasionally and us Boring
Old Farts start reminiscing about "real hardware" <g>.
I know what you're talking about, but the last time I used a
lineprinter, or had compilation output go to one was 1982, and
that was Fortran. :-)


We still had a lineprinter on the VAX in 1983, but that was being
replaced (that was DEC Fortran 77 and Coral 66, plus Bliss, Macro and
other DECnesses). By then other printers were getting cheaper and as
fast (and using less power).

Chris C
Nov 15 '05 #25
Andrey Tarasevich <an**************@hotmail.com> writes:
Giannis Papadopoulos wrote:
...
I think you should first read, then respond...


Huh? I think you should either post something meaningful or not post at
all. What exactly were you trying to say?


His point, I think, was that CBFalconer acknowledged his error a
couple of days ago.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #26
Keith Thompson wrote:
...
I think you should first read, then respond...


Huh? I think you should either post something meaningful or not post at
all. What exactly were you trying to say?


His point, I think, was that CBFalconer acknowledged his error a
couple of days ago.


Well, firstly, it wasn't "a couple of days ago" from my post. It was the same
day I posted my message. And secondly, the very first message where CBFalconer
acknowledges his error (the "oversized floppy hat" one) appeared on my server
hours later than my message (even though the timestamp might indicate
otherwise). That's just how Usenet works.

--
Best regards,
Andrey Tarasevich
Nov 15 '05 #27
CBFalconer wrote:

Giannis Papadopoulos wrote:

[snip... ] [snip...] [snip...]

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.

I missed that point too. Hey, there has only been one
perfect person,
and they crucified him. (From a Christian point of view...)
Nov 15 '05 #28

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.