atrcmp and ==

G

Giannis Papadopoulos

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

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

Chris Croughton

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
 
R

Randy Howard

Chris Croughton wrote
(in article said:
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. :)
 
C

Chris Croughton

Chris Croughton wrote


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

Andrey Tarasevich

Giannis said:
...
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?
 
K

Keith Thompson

Andrey Tarasevich said:
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.
 
A

Andrey Tarasevich

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

Charles Richmond

CBFalconer said:
Giannis said:
[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...)
 

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

No members online now.

Forum statistics

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

Latest Threads

Top