Jump to content
  • Advertisement
Sign in to follow this  
siliconsurfer

Comparing Char's the right way C++

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I know you shouldn't use the "==" operator to compare to strings (or char *)in C++, afterall thats what strcmp is for. But I was wondering is it valid to compare to char's using "=="? Cheers

Share this post


Link to post
Share on other sites
Advertisement
you can compare chars with ==

in c/c++ a char is just a small integer.

with c++ there is a much nicer alternavie to char* and strcmp(),

std::string

you can use == on it

Share this post


Link to post
Share on other sites
Yes, for comparing a single char to another single char, == is perfectly valid. The reason you cannot use it on char* is that that will compare pointer values rather than the contents of the strings. Anyway, if you're using C++ you shouldn't use char* anyway. You should use std::string instead.

Tom

EDIT: predictably someone else beat me to it :)

Share this post


Link to post
Share on other sites
Also, if you used -> to dereference the pointer and used == to compare the values, it would only get the first char in the array, not the whole string. Using the std:string class thingy would probably be better because it overloads the == operator to compare the two strings. Yes, I saw that this was stated above, but I wanted to repeat to state my own opinion as well. I can say that I haven't needed to use the string class myself yet simply because I don't ever need to compare strings, rather I use them to print text for OpenGL. For someting simple like this, I don't recommend use of the class.

Share this post


Link to post
Share on other sites
In a class, maybe?

struct foo{
foo() {bar = new char('b');}
~foo() {delete bar;}

char *bar;
};

int main(){
foo foobar;

char foo2 = foobar->bar;

return 0;
}


This code might and might not work. Its simply a hypothesis using info from many people and libraries.

Share this post


Link to post
Share on other sites
Quote:
Original post by dudedbz1
In a class, maybe?

struct foo{
foo() {bar = new char('b');}
~foo() {delete bar;}

char *bar;
};

int main(){
foo foobar;

char foo2 = foobar->bar;

return 0;
}


This code might and might not work. Its simply a hypothesis using info from many people and libraries.


It will store the address of what bar is pointing to in foo2 (well, probably just part of it since a char is usually a byte and most modern machines have at least a 32-bit word size and it would be pretty much useless to store part of it), I doubt that's what you're going for.

EDIT:
In response to the original question: Yes, you can compare chars with the == operator. The reason you can't compare char pointers that way (well, you can but it probably won't do what you expect) is because it would compare the actually memory addresses of the two arrays not the values since a char* is a pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by dudedbz1
In a class, maybe?

struct foo{
foo() {bar = new char('b');}
~foo() {delete bar;}

char *bar;
};

int main(){
foo foobar;

char foo2 = foobar->bar;

return 0;
}



yeah but that isnt what was suggested

Quote:
Original post by kburkhart84
if you used -> to dereference the pointer and used == to compare the values, it would only get the first char in the array, not the whole string.


Share this post


Link to post
Share on other sites
Quote:
Original post by dudedbz1
*** snippet snipped ***

Lots of troubling things in that code; don't want anybody to be lead astray - let me try and clarify:
struct foo;

int main() {

// create an object and a pointer to foo
foo foo_object; // instanciate a foo object
foo * foo_pointer( new foo ); // point to a newly created foo object

// accessing a foo object
char c( * foo_object.bar ); // initialize a char with object at foo's bar
char * cp( foo_object.bar ); // initialize a char pointer with foo's bar

// accessing a foo object through a pointer
c = * foo_pointer->bar; // assign object at foo's bar
cp = foo_pointer->bar; // assign foo's bar

delete foo_pointer; // be nice :)
return 0;

}
struct foo {

foo(): bar( new char('b') ) {;}
~foo() { delete bar; }

char * bar;
};

Share this post


Link to post
Share on other sites
I know! That guy said that the * was for dereferencing and that the -> was for he had no idea. I took an educated guess.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!