Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String Comparison

Reply
Thread Tools

String Comparison

 
 
Dhruv Ahuja
Guest
Posts: n/a
 
      11-01-2004
Consider:

printf("%d", "ABC" == "ABC");

The ANSI standard says that string comparsion using a comparison
operator (==) is not allowed. It recommends the use of the strcmp() in
<string.h>.

The Borland compiled programs print "0" on this; while the GCC and MS
VC++ compiled programs print "1".

Which compiler, would you say, returns the most appropriate answer,
not forgetting what the ANSI recommends...
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      11-01-2004
Dhruv Ahuja <(E-Mail Removed)> scribbled the following:
> Consider:


> printf("%d", "ABC" == "ABC");


> The ANSI standard says that string comparsion using a comparison
> operator (==) is not allowed. It recommends the use of the strcmp() in
> <string.h>.


It certainly does not say it is not allowed. I think what it says is
that it will not necessarily yield the correct result based on the
strings' content.

> The Borland compiled programs print "0" on this; while the GCC and MS
> VC++ compiled programs print "1".


> Which compiler, would you say, returns the most appropriate answer,
> not forgetting what the ANSI recommends...


All. None. Pick one. Or more. All the ANSI standard specifies in this
regard is that if two strings have different contents, an == comparison
must return 0. If they have the same content, it might return either 0
or 1, completely depending on the implementation.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      11-01-2004

Dhruv Ahuja wrote:
> Consider:
>
> printf("%d", "ABC" == "ABC");
>
> The ANSI standard says that string comparsion using a comparison
> operator (==) is not allowed.


Please quote the standard where it says this.

> It recommends the use of the strcmp() in
> <string.h>.


Yeah, if you want to get an answer that means what you think it does.

> The Borland compiled programs print "0" on this; while the GCC and MS
> VC++ compiled programs print "1".


The comparison you show checks the equality of two pointers. If a
particular compiler has reused the string literal, then indeed the
pointers will compare because they will be the same one. If it
generates a unique string for each literal, then they won't.

> Which compiler, would you say, returns the most appropriate answer,
> not forgetting what the ANSI recommends...


As the standard doesn't recommend anything for the case you show,
either is appropriate.


Brian

 
Reply With Quote
 
=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=
Guest
Posts: n/a
 
      11-01-2004
On Mon, 01 Nov 2004 20:37:49 +0000, Joona I Palaste wrote:

> Mark McIntyre <(E-Mail Removed)> scribbled the following:
>> On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
>> <(E-Mail Removed)> wrote:
>>>All the ANSI standard specifies in this
>>>regard is that if two strings have different contents, an == comparison
>>>must return 0.

>
>> Only by coincidence, since the values of the pointers to the two will more
>> or less have be different. I guess its possible to concieve of an
>> implementation or mechanism where this would not be true.

>
> If it is, *I'd* be glad to hear of it. Wouldn't this require memory
> behaving differently depending on which pointer it's accessed through?

"one\0two" and "one" might compare equal with some compilers/linkers.


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-01-2004
On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
<(E-Mail Removed)> wrote:

>All the ANSI standard specifies in this
>regard is that if two strings have different contents, an == comparison
>must return 0.


Only by coincidence, since the values of the pointers to the two will more
or less have be different. I guess its possible to concieve of an
implementation or mechanism where this would not be true.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      11-01-2004
Mark McIntyre <(E-Mail Removed)> scribbled the following:
> On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
> <(E-Mail Removed)> wrote:
>>All the ANSI standard specifies in this
>>regard is that if two strings have different contents, an == comparison
>>must return 0.


> Only by coincidence, since the values of the pointers to the two will more
> or less have be different. I guess its possible to concieve of an
> implementation or mechanism where this would not be true.


If it is, *I'd* be glad to hear of it. Wouldn't this require memory
behaving differently depending on which pointer it's accessed through?

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      11-01-2004
In article <cm66qt$1qk$(E-Mail Removed)>,
Joona I Palaste <(E-Mail Removed)> wrote:
>Mark McIntyre <(E-Mail Removed)> scribbled the following:
>> On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
>> <(E-Mail Removed)> wrote:
>>>All the ANSI standard specifies in this
>>>regard is that if two strings have different contents, an == comparison
>>>must return 0.

>
>> Only by coincidence, since the values of the pointers to the two will more
>> or less have be different. I guess its possible to concieve of an
>> implementation or mechanism where this would not be true.

>
>If it is, *I'd* be glad to hear of it. Wouldn't this require memory
>behaving differently depending on which pointer it's accessed through?


Not allowed to happen. If two pointers compare equal, following them
will give you the same object, which has to have the same value whichever
pointer you use to get at it.

Note that this only applies to equality conversions - it's possible to
have two different pointers (to nonidentical strings, f'rexample) such
that ptr1<ptr2 and ptr2<ptr1 both evaluate to false (which if pointers
were well-ordered would imply that they were equal).
(This can happen in a fully segmented memory model where only the offsets
into the segment are used for inequality comparisons, which is valid
since pointer inequality comparisons are only defined on pointers into
the same object or array. Comparing for equal or not-equal doesn't have
this constraint and would be required to check both segment and offset,
coming back with not-equal in this case.)


dave

--
Dave Vandervies http://www.velocityreviews.com/forums/(E-Mail Removed)
Now, obviously I've picked a terrible example, ... But if I picked something
complicated like error-diffusion, it would be... well... complicated.
--Arthur J. O'Dwyer in comp.programming
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      11-01-2004
In article <(E-Mail Removed)>,
Nils O. Selåsdal <(E-Mail Removed)> wrote:

>"one\0two" and "one" might compare equal with some compilers/linkers.


Since we're talking about strings, which are defined as sequences of
non-'\0' characters terminated by a '\0' character, these are the same
string, so they're allowed to be pointed at by equal pointers.


dave

--
Dave Vandervies (E-Mail Removed)
Now, obviously I've picked a terrible example, ... But if I picked something
complicated like error-diffusion, it would be... well... complicated.
--Arthur J. O'Dwyer in comp.programming
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      11-01-2004
On 1 Nov 2004 20:37:49 GMT, in comp.lang.c , Joona I Palaste
<(E-Mail Removed)> wrote:

>Mark McIntyre <(E-Mail Removed)> scribbled the following:
>> On 1 Nov 2004 18:55:05 GMT, in comp.lang.c , Joona I Palaste
>> <(E-Mail Removed)> wrote:
>>>All the ANSI standard specifies in this
>>>regard is that if two strings have different contents, an == comparison
>>>must return 0.

>
>> Only by coincidence, since the values of the pointers to the two will more
>> or less have be different. I guess its possible to concieve of an
>> implementation or mechanism where this would not be true.

>
>If it is, *I'd* be glad to hear of it. Wouldn't this require memory
>behaving differently depending on which pointer it's accessed through?


One trivial way to achieve this is to pass pointers between processes,
since the same logical address could map to different physical ones. This
is of course outside the realms of C.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
False Positives From String Comparison using string.Equals() Smithers ASP .Net 12 07-07-2009 01:23 AM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Is there systematic performance comparison of std::string and c style string? yu_kuo@sina.com C++ 21 08-18-2007 04:32 PM
simple jsp string comparison Jason Java 2 09-20-2003 12:16 AM



Advertisments