468,268 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

String constant in compare operation


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?

Nov 20 '08 #1
4 2455
C. J. Clegg wrote:
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.
Nov 20 '08 #2
C. J. Clegg <an*****************@no.spamwrites:
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) ks***@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"
Nov 20 '08 #3
C. J. Clegg wrote:
>
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
Nov 20 '08 #4
On Thu, 20 Nov 2008 23:18:22 +0000, Mark McIntyre
<ma**********@TROUSERSspamcop.netwrote:
>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...

Nov 21 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Roose | last post: by
8 posts views Thread by Jason Heyes | last post: by
10 posts views Thread by lovecreatesbea... | last post: by
35 posts views Thread by dtschoepe | last post: by
35 posts views Thread by Smithers | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.