Comparing strings

Started by
6 comments, last by DividedByZero 11 years, 8 months ago
I have a non-terminated string of variable size, being read from a file and being copied into a buffer (see the following).

char szParam0[32];
b_file >> str;
strcpy(szParam0,str);


In this case szParam0=="player"

The problem that I am having is that when I try to compare with if() statements the result is never true.

if(szParam=="player") // Never equals true as szParam is a 32 byte char buffer.

Knowing that the string held in szParam can be variable length how can I use the if statement to compare against another string of text?

Any help would be greatly appreciated.
Advertisement
You can either use the strcmp function or use std::string, which also provides for string comparisons.
I assume you mean "NUL terminated", not "non" terminated.

To be clear about why this doesn't work, you are comparing an array with a pointer to a literal string. In this circumstance, the array decays to a pointer to the first element. The literal string exists somewhere in the executable data, it will never occupy the same place as the array, so the comparison will always fail.

As always, trying to use raw character arrays as strings is hazardous for beginners. They make it too easy to do the wrong thing. Unless you have an exceptionally good reason, you should always use std::string for this.

In particular, reading unconstrained number of characters from an external source into a fixed length buffer is a security risk. A well crafted file could result in the execution of arbitrary code.
To add on what RulerOfNothing noted, there is a C standard function called strcmp

use it like this

if ( strcmp(str1, str2) == 0) do some stufff.....


it returns an integer, if it is zero then the strings are equivalent. From what i know it will return a negative number if the first string is less than the second (the first nonequivalent letter/character has an ASCII number less than the second strings) and will return positive if the first nonequivalent letter in the first has an ASCII number greater than the seconds

basically like this:

  1. strcmp(s1, s2) < 0 /// s1 < s2
  2. strcmp(s1, s2) > 0 //// s1> s2
  3. strcmp(s1, s2) ==0 //// s1 == s2


the library for this function should be string.h or stdlib.h (possibly cctype.h) but im not really sure


and as stated by rip-off, it is much safer to use C++ strings instead. You dont need a special function, s1==s2 is defined for those strings, as is a multitude of varying features. just #include <string> (not string.h, the C-style one) and you're ready to go.
Always improve, never quit.
I have a non-terminated string [...]


'sz' is the Hungarian prefix for a zero-terminated string. Only use it if you indeed mean what rip-off has assumed.

The problem that I am having is that when I try to compare with if() statements the result is never true.


[source lang="cpp"]if(szParam == "player") /* compare the address of szParam to the location where "player" is stored */[/source]

String literals are actually constant pointers. It isn't possible to compare them this way because you're trying to compare two pointers and not the data they point to.

The easiest method would be to use strncmp in the Standard C library. It obeys the rules that arkane7 stated but allows you to specify a maximum number of characters to compare.

[source lang="cpp"]if(strncmp(szParam, "player", sizeof("player")) == 0)
{
/* the two strings are equal */
}[/source]

Remember that str{n}cmp is a character-by-character check of two strings. If one string is longer than the other you'll flow over the end of the smaller string.
The literal string exists somewhere in the executable data, it will never occupy the same place as the array, so the comparison will always fail.g.gif
I'm not sure if this has anything to do with what you're looking for because im not amazing with C++

but in my text game i have something like

cin >> response;

if(response.compare("yes"))
{
do stuff
}
else
{
do other stuff
}
/********************************************************************************\
/**********************He Who Dares, Wins**********************************\
/********************************************************************************\
Thanks guys, you are all awesome, +1 to each of you :)

This topic is closed to new replies.

Advertisement