Connecting Tech Pros Worldwide Help | Site Map

String constant in compare operation

  #1  
Old November 20th, 2008, 11:15 PM
C. J. Clegg
Guest
 
Posts: n/a

Consider this code:

char* s;
char* putSomethingThere( void );

s = putSomethingThere( );

if( s == "abc" )
{
...
}

If I remember correctly, that's correct C++ but incorrect C, even if
putSomethingThere() put "abc" into s, right?

  #2  
Old November 20th, 2008, 11:25 PM
Mark McIntyre
Guest
 
Posts: n/a

re: String constant in compare operation


C. J. Clegg wrote:
Quote:
Consider this code:
>
char* s;
char* putSomethingThere( void );
>
s = putSomethingThere( );
>
if( s == "abc" )
{
...
}
>
If I remember correctly, that's correct C++ but incorrect C, even if
putSomethingThere() put "abc" into s, right?
Its not 'correct' in either language, if your intention is to compare
the strings. In both cases it compares the value of the pointers to the
start of the strings.

You may be thinking in C++ of the std::string class which AFAIR has an
== operator defined on it to permit comparison to a string literal.
  #3  
Old November 20th, 2008, 11:35 PM
Keith Thompson
Guest
 
Posts: n/a

re: String constant in compare operation


C. J. Clegg <answer.in.newsgroup@no.spamwrites:
Quote:
Consider this code:
>
char* s;
char* putSomethingThere( void );
>
s = putSomethingThere( );
>
if( s == "abc" )
{
...
}
>
If I remember correctly, that's correct C++ but incorrect C, even if
putSomethingThere() put "abc" into s, right?
The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've just asked
question 8.2.

<OFFTOPIC>
The above code probably means the same thing in C++ as it does in C,
but C++ has other features that let you use the "==" operator to
compare strings. For details, consult a C++ textbook; if that fails,
ask in comp.lang.c++.
</OFFTOPIC>

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  #4  
Old November 20th, 2008, 11:35 PM
Default User
Guest
 
Posts: n/a

re: String constant in compare operation


C. J. Clegg wrote:
Quote:
>
Consider this code:
>
char* s;
char* putSomethingThere( void );
>
s = putSomethingThere( );
>
if( s == "abc" )
{
...
}
>
If I remember correctly, that's correct C++ but incorrect C, even if
putSomethingThere() put "abc" into s, right?
You'll have to define what you mean by "correct". There are no syntax
errors that I see, although the definition for putSomethingThere() is
missing. However, comparing strings with == is almost always a design
flaw, as you will be comparing the pointer values, not what they point
to. It's the same for C++, although you might be thinking of the
std::string class.




Brian
  #5  
Old November 21st, 2008, 12:45 AM
C. J. Clegg
Guest
 
Posts: n/a

re: String constant in compare operation


On Thu, 20 Nov 2008 23:18:22 +0000, Mark McIntyre
<markmcintyre@TROUSERSspamcop.netwrote:
Quote:
>You may be thinking in C++ of the std::string class which AFAIR has an
>== operator defined on it to permit comparison to a string literal.
Indeed, that's what I was thinking of. I forgot that C++ had an
overloaded == operator to permit that.

Thanks to all...

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
My hash table is in need of peer review Johan Tibell answers 21 August 20th, 2006 01:35 AM
PEP 354: Enumerations in Python Ben Finney answers 77 March 3rd, 2006 09:35 AM
STL list.size() operation - O(1) or O(n) ? Brett L. Moore answers 12 July 19th, 2005 04:25 PM
String replace with return value? Roose answers 5 July 18th, 2005 05:24 PM