Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


perhaps i've been staring at this for too long...

This topic is 6080 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

otay fellas, heres the situation... im writing a console class for debugging and stuff, and its all done except for one small problem. the function im using to execute the commands isnt working properly.. without further ado, here is some code:
int CConsole::Command(char lparam[1024], char wparam[1024])
	if(lparam == "TOGGLEFS") 
		AddLine("-- Fullscreen Mode Toggled");
        // note, this is just a lil snippet of what ive got, didnt

        // want to bog anybody down

	return (0);
ok, the string im passing to command is a private class variable szCommand[1024]. when i call MyConsole.Command(szCommand, NULL); nothing happens. yet- when i call MyConsole.Command("TOGGLEFS", NULL); it works perfectly.. is there something im missing? i know im gonna be slapping myself after this. hopefully anyway.. heh -eldee ;another space monkey; [ Forced Evolution Studios ]

Vash says: eat more donuts, play more games! Edited by - eldee on February 23, 2002 8:26:56 PM

Share this post

Link to post
Share on other sites
You cannot compare C-style strings with ==. This will only compare the actual addresses the char pointers are pointing to. So if the passed in string is stored at 0x0001a000 and the string constant "TOGGLEFS" is stored at 0x0020b000, the comparison will fail no matter how equal the strings are.
Use the strcmp standard C function for comparing two strings like this.
The reason it works when you do pass in a constant string is because the compiler is intelligent enough to recognize that the two constants are equal and to put just one instance of it in the area of memory allocated for static data.

Share this post

Link to post
Share on other sites
Thou shalt not compare char*s with ==
Thou shalt use strcmp() instead.

That''s because even if your variable contains the same text as the constant "TOGGLEFS", it does NOT point to the same place.

The compiler usually places one single "TOGGLEFS" string somewhere in memory and all instances of "TOGGLEFS" in your code actually are pointers to that place (which means that, yes, "TOGGLEFS"[5] is equal to ''E'').

But that''s not always true, other compilers may keep several copies, which would mean that "TOGGLEFS" == "TOGGLEFS" could be false

Share this post

Link to post
Share on other sites
i knew i could count on you guys
i learn something new every day.. thats what i love about
this stuff. always something new to frustrate and confuse, and
eventually overcome.

Share this post

Link to post
Share on other sites
just make sure that when you use strcmp(), not to do it like this:

if (!strcmp(str1,str2) // code here

it should be:

if (strcmp(str1,str2) != 0) // code here, if strcmp() returns non zero, the strings dont match(zero is a match)

Share this post

Link to post
Share on other sites
Remember, in C, expressions are evaluated, some at compile time, others at runtime.

"This is a string" is an expression. So is MY_CONSTANT. So is myVariable. So is 3 + 4 * x. So is myFunction(x). Those are all expressions.

Remember that a quoted string is an expression which is evaluated at compile time to the pointer which points to the strnig.

So, look at this expression: strcmp ("Apples, myVariable);

"Apples" evaluates to the pointer which points to where the characters "Apples" reside at. myVariable evaluates to whatever myVariable contains. These are passed into the function strcmp().

Look at: char *stuff = "Apples and oranges";

"Apples and oranges" evaluates to the pointer where the string resides. That makes for an easy assignmnet into the character pointer *stuff.

Look at: char stuff[32] = "Apples and oranges";

The above statement is the ONE exception to the way the compiler handles strings. It will let you slide on this particular syntac because it is convenient. In this case, it will assign the letters of the string into your character buffer called stuff.


Share this post

Link to post
Share on other sites
Original post by Kylotan
Of course, since you're using C++, you'd probably be better served by investigating the standard string class, which does allow == comparisons, plus a whole lot more.

yeah, a few months ago i first dipped my hand into the string
class, and i ran into a few problems. (the same problems may
exist with char's, but im more familiar with how they work).
the main problem was speed...
i had a nested for loop that looked somethin like this:

int i, j;
for(int y = 0; y < 200; y++)
for(int x = 0; x < 200; x++)
i = tempstring.find("&",i) + 2;
j = tempstring.find("?",i+1);

if (j > 0)
sHex = mid(tempstring, i, j - i);
Terrain[x][y].surface = clng(sHex);
i = j;

like i said, that's probably slow no matter how you do it,
but i was scared to mess with it after that
i'll probably pick it up again after this project im working on
now, i just wanted this project to be something i'd be
totaly familiar with inside out.

;another space monkey;
[ Forced Evolution Studios ]

Vash says: eat more donuts, play more games!

[Edit: You know you're anal when you go back and fix your pseudo-code's compile errors]

Edited by - eldee on February 24, 2002 7:23:54 PM

Share this post

Link to post
Share on other sites

  • 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!