PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > strcmp, please me understand this
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
strcmp, please me understand this

Réponse
 
LinkBack Outils de la discussion
Vieux 27/06/2008, 18h58   #1
Marcus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut strcmp, please me understand this

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
  Réponse avec citation
Vieux 27/06/2008, 19h04   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

* 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?


The code you have shown should not compile.

Show the real code.

Remember: first convert tabs to spaces (if you have tabs), then copy and paste
the code into your article -- don't retype the code.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 27/06/2008, 19h11   #3
Mike Wahler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this


"Marcus" <mcdesigns@walla.com> wrote in message
news:c8492b59-57c9-4875-832d-310d8ae1047c@y22g2000prd.googlegroups.com...
> hi, i'm really confused as to what's going on here:
>
> char *foo = "ABC";
> char *foo2 = "ABC;


Presumably you meant

char *foo2 = "ABC";

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


Did you mean

printf("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?


Impossible to say from the incomplete example.

Post a small, compilable program that produces
the problem, and I'm sure someone can find the
trouble.

-Mike


  Réponse avec citation
Vieux 27/06/2008, 19h13   #4
Marcus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

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.

> The code you have shown should not compile.
>
> Show the real code.
>
> Remember: first convert tabs to spaces (if you have tabs), then copy and paste
> the code into your article -- don't retype the code.
>
> Cheers, & hth.,
>
> - Alf
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?


  Réponse avec citation
Vieux 27/06/2008, 19h23   #5
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

Marcus <mcdesigns@walla.com> wrote in news:c8492b59-57c9-4875-832d-
310d8ae1047c@y22g2000prd.googlegroups.com:

> 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
  Réponse avec citation
Vieux 27/06/2008, 19h53   #6
Marcus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

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.
  Réponse avec citation
Vieux 27/06/2008, 19h57   #7
Mike Wahler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this


"Marcus" <mcdesigns@walla.com> wrote in message
news:f14ccdd1-fa7e-4c5e-ab0a-5ffd6b5a09ec@t12g2000prg.googlegroups.com...
> 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.


No, the code you posted does not compile. If you
really want , please listen to what Alf (and I)
are telling you. Post a *complete*, *compilable*
program (ALL of it, including header #includes),
or we simply cannot you.

-Mike


  Réponse avec citation
Vieux 27/06/2008, 20h02   #8
Mike Wahler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this


"Marcus" <mcdesigns@walla.com> wrote in message
news:c8492b59-57c9-4875-832d-310d8ae1047c@y22g2000prd.googlegroups.com...
> 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?


I (and others) have addressed your 'strcmp()' issues,
but I should have pointed out:

This is C++. There is a *much* better way to compare
strings: use std::string objects.

#include <string>
#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;
}

No mucking around with pointers, with the associated
hazards.

-Mike


  Réponse avec citation
Vieux 27/06/2008, 20h09   #9
Mike Wahler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this


"Marcus" <mcdesigns@walla.com> wrote in message
news:b4aea5d2-b49b-43a7-ab39-831c31b26de8@u36g2000prf.googlegroups.com...
> you guys are funny. i thought this was comp.lang.c++... not
> comp.we.can.only.comment.on things.that.can.be.compiled.c++


Huh? This is a C++ group. So of course we talk about
C++. Often there are discussions about C++ issues where
actual code is not posted. BUT: you asked about specific
function's behavior under certain circumstances. So of
course we need to know *exactly* what you did.

>
> 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."


This is essentially the same as we're telling you. "the error is
somewhere else." If we don't see what the "somewhere else" looks
like, we're only guessing, and wasting everyone's time.

>
> times change i guess. thanks though joe, appreciate you taking the
> time to actually compile that.


Please note that he did NOT compile YOUR code. He simply wrote
(I assume correct) code, and compiled THAT. He cannot know what
the rest of your code really looks like.

> the error must be somewhere else.


Of course it is. But if you refuse to show us "somewhere else",
we're only guessing. I won't do that.

You're coming awfully close to "biting a hand that feeds you."

-Mike


  Réponse avec citation
Vieux 27/06/2008, 20h49   #10
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

Marcus <mcdesigns@walla.com> wrote in news:b4aea5d2-b49b-43a7-ab39-
831c31b26de8@u36g2000prf.googlegroups.com:

> 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
  Réponse avec citation
Vieux 29/06/2008, 14h33   #11
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

Marcus wrote:
> 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.
  Réponse avec citation
Vieux 30/06/2008, 04h58   #12
CD1
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

> #include <string>
> #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".


  Réponse avec citation
Vieux 30/06/2008, 06h04   #13
Marcus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

On Jun 29, 8:58pm, CD1 <cristiandei...@gmail.com> wrote:
> > #include <string>
> > #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".
>
> - 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 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
  Réponse avec citation
Vieux 30/06/2008, 08h08   #14
Frank Birbacher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

Hi!

Marcus schrieb:
> 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
  Réponse avec citation
Vieux 30/06/2008, 10h58   #15
Yannick Tremblay
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

In article <b4aea5d2-b49b-43a7-ab39-831c31b26de8@u36g2000prf.googlegroups.com>,
Marcus <mcdesigns@walla.com> wrote:
>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.


Why do newbies insist on being childish? Peoples up here are willing
to . However, they can only with the material they have.
But no, so often, newbies when they don't get it all cooked,
pre-chewed and spoonfed directly in their mouth, fell the need to
throw a tantrum.

Look, you came here and asked for . However, you didn't ask a
clear question ("It doesn't work"). Why are _you_ unwilling to make an
effort yourself so that others can _you_ solve _your_ problem?


Essentially, all that can be answered from your post:

1- strcmp works.
2- There must be an error somewhere in your code.
3- The sample you have posted is not complete and would not
compile if inserted in a real program.

The above is really not that useful to you: i.e. strcmp works. That
should be a given. But you've have made a mistake. If you post a
complete but minimal self contained example that actually compile,
then peoples can look and identify where you made the mistake. If you
don't do that, all that can be said is: "You made a mistake somewhere."


Yannick

  Réponse avec citation
Vieux 30/06/2008, 11h11   #16
Lionel B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

On Sun, 29 Jun 2008 22:04:31 -0700, Marcus wrote:

> On Jun 29, 8:58Âpm, CD1 <cristiandei...@gmail.com> wrote:
>> > #include <string>
>> > #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".
>>
>> - 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!
>


He was making the reasonable and potentially ful 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 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 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 .

--
Lionel B
  Réponse avec citation
Vieux 30/06/2008, 11h38   #17
Eberhard Schefold
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

Marcus wrote:

> thanks for the 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 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?
  Réponse avec citation
Vieux 30/06/2008, 15h51   #18
Marcus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

On Jun 30, 3:38am, Eberhard Schefold <eberhard.schef...@de.bosch.com>
wrote:
> Marcus wrote:
> > thanks for the 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 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?
  Réponse avec citation
Vieux 30/06/2008, 16h27   #19
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

In article <ad4e6803-e381-4a5d-b4d7-ed1554319df3
@g16g2000pri.googlegroups.com>, mcdesigns@walla.com says...

[ ... ]

> 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).

--
Later,
Jerry.

The universe is a figment of its own imagination.
  Réponse avec citation
Vieux 30/06/2008, 16h41   #20
Lionel B
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

On Mon, 30 Jun 2008 07:51:51 -0700, Marcus wrote:

> On Jun 30, 3:38Âam, Eberhard Schefold <eberhard.schef...@de.bosch.com>
> wrote:
>> Marcus wrote:
>> > thanks for the 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 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.


[...]

> 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-lit...t.html#faq-5.8

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

--
Lionel B
  Réponse avec citation
Vieux 30/06/2008, 16h51   #21
Eberhard Schefold
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: strcmp, please me understand this

Marcus wrote:

> 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 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,37332 seconds with 29 queries