strcmp, please help me understand this

M

Marcus

hi, i'm really confused as to what's going on here:

char *foo = "ABC";
char *foo2 = "ABC;

if(strcmp(foo,foo2) == 0)
print("Works");

it doesn't print, when i step into strcmp the values in strcmp.asm are
always str1 "ABC", str2 "BÍÍÍ". it doesn't matter the order str2 is
always junk.

how can this be? am i not understanding how strcmp works?

thanks
 
M

Marcus

char *foo = "ABC";
char *foo2 = "ABC";

if(strcmp(foo,foo2) == 0)
printf("Works");

i missed a quote in foo2 and meant printf. sorry it became completely
meaningless with those errors.

the basic point is that char*'s aren't strcmp'ing and i'm not sure
why. my code compiles.
 
J

Joe Greer

hi, i'm really confused as to what's going on here:

char *foo = "ABC";
char *foo2 = "ABC;

if(strcmp(foo,foo2) == 0)
print("Works");

it doesn't print, when i step into strcmp the values in strcmp.asm are
always str1 "ABC", str2 "BÍÍÍ". it doesn't matter the order str2 is
always junk.

how can this be? am i not understanding how strcmp works?

thanks

After fixing the errors, your code above works fine for me. So, what is
the real code?

joe
 
M

Marcus

you guys are funny. i thought this was comp.lang.c++... not
comp.we.can.only.comment.on things.that.can.be.compiled.c++

back in the day in this group, the jibe replies would've looked like:
"char*'s WILL strcmp, assuming you didn't mean those errors above,
your error is somewhere else."

times change i guess. thanks though joe, appreciate you taking the
time to actually compile that. the error must be somewhere else.
 
J

Joe Greer

you guys are funny. i thought this was comp.lang.c++... not
comp.we.can.only.comment.on things.that.can.be.compiled.c++

back in the day in this group, the jibe replies would've looked like:
"char*'s WILL strcmp, assuming you didn't mean those errors above,
your error is somewhere else."

times change i guess. thanks though joe, appreciate you taking the
time to actually compile that. the error must be somewhere else.

The point is that strcmp works as advertised, so there is something else
going on corrupting things. Since you are messing with pointers, I would
look for boundary overwrites or writing to invalid pointers. A tool like
Purify can be valuable for finding these sorts of things.

joe
 
J

Juha Nieminen

Marcus said:
you guys are funny. i thought this was comp.lang.c++... not
comp.we.can.only.comment.on things.that.can.be.compiled.c++

back in the day in this group, the jibe replies would've looked like:
"char*'s WILL strcmp, assuming you didn't mean those errors above,
your error is somewhere else."

What's wrong with that answer? It looks perfectly accurate to me.

Fixing your typing mistakes and putting a minimal complete program
around it makes it work as it should. Thus your lines don't show any
error and thus it's impossible to answer your question.
 
C

CD1

#include said:
#include <iostream>

int main()
{
    std::string foo("ABC");
    std::string f002("ABC");

    if(foo == foo2)
        std::cout << "match\n";
    else
        std::cout << "no match\n";

    return 0;

}

This code won't compile. There is no variable called "foo2".

:)
 
M

Marcus

This code won't compile. There is no variable called "foo2".

:)- Hide quoted text -

- Show quoted text -

haha.... i wasn't going to say anything, but that made me laugh... yea
mike, i simply can't understand what you're trying to say here at
all! ;)

thanks for the help guys, seriously. i'm just trying to point out,
that communication is a lot easier when the listener tries to
understand the meaning behind things, rather than dismissing the
entirety if an i wasn't dotted or a t wasn't crossed. i think yall get
what i'm saying... or did i miss an apostophe somewhere?

regards,
marcus
 
F

Frank Birbacher

Hi!
hi, i'm really confused as to what's going on here:

char *foo = "ABC";
char *foo2 = "ABC;

Change these to:

const char* const foo = "ABC";
const char* const foo2 = "ABC";

And try again. If this does not discover your problem then you are in
real trouble.
how can this be? am i not understanding how strcmp works?

It should work as expected but you seem to mess with the data somewhere.
Somewhere we can't see.

Frank
 
L

Lionel B

haha.... i wasn't going to say anything, but that made me laugh... yea
mike, i simply can't understand what you're trying to say here at all!
;)

He was making the reasonable and potentially helpful suggestion that you
might consider using the the std::string class rather than char*, as it
makes comparison much simpler and less error-prone.
thanks for the help guys, seriously. i'm just trying to point out, that
communication is a lot easier when the listener tries to understand the
meaning behind things, rather than dismissing the entirety if an i
wasn't dotted or a t wasn't crossed. i think yall get what i'm saying...
or did i miss an apostophe somewhere?

Seeing as you're asking a bunch of total strangers to take the time and
trouble to help you, don't you think it might be a good idea to make it
easier for them to do so? This is why people request complete, compilable
code: it makes it easier to help.
 
E

Eberhard Schefold

Marcus said:
thanks for the help guys, seriously. i'm just trying to point out,
that communication is a lot easier when the listener tries to
understand the meaning behind things, rather than dismissing the
entirety if an i wasn't dotted or a t wasn't crossed. i think yall get
what i'm saying... or did i miss an apostophe somewhere?

These guys were trying to help you, and they can't if you don't tell
them what the problem is. The misunderstanding is on your own part if
you regard this as nitpicking. They were asking what the problem is,
since your initial code obviously was not apt to demonstrate the problem.

If a stranger came to you on the street and asked you "Where's Zelig
Street?", and you're answering "There is no Zelig Street here, are you
sure you got the right name?", and he started complaining over your
manners, that you're not listening, etc., what would you think?
 
M

Marcus

These guys were trying to help you, and they can't if you don't tell
them what the problem is. The misunderstanding is on your own part if
you regard this as nitpicking. They were asking what the problem is,
since your initial code obviously was not apt to demonstrate the problem.

If a stranger came to you on the street and asked you "Where's Zelig
Street?", and you're answering "There is no Zelig Street here, are you
sure you got the right name?", and he started complaining over your
manners, that you're not listening, etc., what would you think?

I was clear. I asked what I wasn't understanding about how strcmp
works, specifically with char*'s. I gave an example using foo, but
flubbed a quote and print instead of printf. No one until AFTER I
quipped that those were nitpicky errors tried to actually answer my
question. It IS nitpicking, just like it would be nitpicking for me to
say that Mike makes no sense because he accidently used f002 instead
of foo2... it's stupid for me to INTENTIONALLY not try and understand
what he's saying about using string's instead of char*'s. I looked
past the error and understood what he was trying to say, this is how
basic communication works with simple subjects. If you want to make
things COMPLICATED and TENSE, then yea, pretending you don't
understand is the right way to go about things.

Your analogy isn't what happend, more like, the stranger asked where
zelig street was with a thick Mexican accent, and I answered, "learn
how to speak english, i can't understand you"... even though I know
exactly what he was saying... it's just, well, any Mexican who can't
pronunciate perfectly shouldn't be in this country, right?
 
J

Jerry Coffin

[ ... ]
I was clear. I asked what I wasn't understanding about how strcmp
works, specifically with char*'s. I gave an example using foo, but
flubbed a quote and print instead of printf. No one until AFTER I
quipped that those were nitpicky errors tried to actually answer my
question. It IS nitpicking, just like it would be nitpicking for me to
say that Mike makes no sense because he accidently used f002 instead
of foo2...

While it's (sort of) true that what they did was mostly nitpicking,
doing so was a lot more reasonable than you seem to realize. In
particular, even if it looked like nitpicking, it pointed out a much
more fundamental problem with your post.

Your question _should_ have been accompanied by complete, compilable
code that demonstrates the problem. That, however, is only meaningful
when/if we're sure that what we're looking at is _precisely_ the same
code that you're having a problem with. The fact that you had print
instead of printf shows that what you posted was NOT precisely the same
as the code you were dealing with -- and it's impossible for us to be
certain that something on the same order (e.g. another typo) wasn't the
source of the real problem.

As such, when you post a problem like this, it's important that you cut
and paste or insert the text of the code directly from the file you
tried to compile, or something on that order. Retyping the code into the
article (which is quite apparently what you did) when you post it may
easily fix the problem you had in the first place, and/or may easily
leave out the part that really caused the problem you were experiencing
(which is what seems to have happened here).
 
L

Lionel B

[...]
I was clear. I asked what I wasn't understanding about how strcmp works,
specifically with char*'s. I gave an example using foo, but flubbed a
quote and print instead of printf. No one until AFTER I quipped that
those were nitpicky errors tried to actually answer my question.

As I see it, it turned out that -- modulo your nitpicky errors -- the
problem *wasn't* in the code fragment you showed us. If you had made the
basic effort to construct a complete, minimal, compilable, program as per
the FAQ for this ng:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

then you would have immediately spotted this for yourself and wouldn't
have had to post here at all!
 
E

Eberhard Schefold

Marcus said:
I was clear. I asked what I wasn't understanding about how strcmp
works, specifically with char*'s. I gave an example using foo, but
flubbed a quote and print instead of printf. No one until AFTER I
quipped that those were nitpicky errors tried to actually answer my
question. It IS nitpicking, just like it would be nitpicking for me to
say that Mike makes no sense because he accidently used f002 instead
of foo2...

That your code didn't compile was the clear indication that you were not
even presenting the actual offending code snippet, let alone the
compilable minimal example that is recommended in such cases. That's
what people were trying to tell you. They can't help you find a solution
if you don't give them the problem. There was (and still is) no way to
extrapolate the problem from what you posted.
Your analogy isn't what happend,

Right! It's more like "Tell me where Zigzag-whatever Street is. I have
the exact name in my notebook but I can't be bothered to look it up and
tell you. --- (People used to be a lot friendlier in this neighborhood.
Nobody listens anymore.)" :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top