472,958 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 2566
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.