473,398 Members | 2,212 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,398 software developers and data experts.

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 2580
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
33
by: desktop | last post by:
In the C++ standard sec 23.1.2 table 69 it says that erase(q) where q is a pointer to an element can be done in amortized constant time. I guess that is not worst case since std::set is...
35
by: Smithers | last post by:
I have been told that it is a good idea to *always* declare string variables with a default value of string.Empty - for cases where an initial value is not known... like this: string myString =...
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.