|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
* 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? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
> #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". ![]() |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#20 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#21 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|