Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing character arrays and string literals in C++

I have a character array and I'm trying to figure out if it matches a string literal, for example:

char value[] = "yes";
if(value == "yes") {
   // code block
} else {
   // code block
}

This resulted in the following error: comparison with string literal results in unspecified behavior. I also tried something like:

char value[] = "yes";
if(strcmp(value, "yes")) {
   // code block
} else {
   // code block
}

This didn't yield any compiler errors but it is not behaving as expected.

like image 369
Ian Burris Avatar asked Oct 28 '09 19:10

Ian Burris


People also ask

What is the difference between character array and string literal?

String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable. Character Arrays are mutable.

Can you compare character arrays in C?

The strcmp() function is a built-in library function in the C language. Its basic role is to compare two character arrays or strings terminated by null value (C-strings) lexicographically. The strcmp() function is called using two character arrays as parameters and returns an integer value.

Can you compare string literals in C?

In C, you can compare single characters (chars) by using the comparion operator ==, however, this method is not valid for comparing arrays of chars, or strings. Instead, you must use a function that compares each of the chars within the arrays in turn.

How do you compare character arrays?

You can compare char arrays that are supposed to be strings by using the c style strcmp function. In C++ you normally don't work with arrays directly. Use the std::string class instead of character arrays and your comparison with == will work as expected.


3 Answers

Check the documentation for strcmp. Hint: it doesn't return a boolean value.

ETA: == doesn't work in general because cstr1 == cstr2 compares pointers, so that comparison will only be true if cstr1 and cstr2 point to the same memory location, even if they happen to both refer to strings that are lexicographically equal. What you tried (comparing a cstring to a literal, e.g. cstr == "yes") especially won't work, because the standard doesn't require it to. In a reasonable implementation I doubt it would explode, but cstr == "yes" is unlikely to ever succeed, because cstr is unlikely to refer to the address that the string constant "yes" lives in.

like image 156
David Seiler Avatar answered Oct 10 '22 09:10

David Seiler


std::strcmp returns 0 if strings are equal.

like image 36
Cat Plus Plus Avatar answered Oct 10 '22 10:10

Cat Plus Plus


strcmp returns a tri-state value to indicate what the relative order of the two strings are. When making a call like strcmp(a, b), the function returns

  • a value < 0 when a < b
  • 0 when a == b
  • a value > 0 when a > b
like image 3
R Samuel Klatchko Avatar answered Oct 10 '22 08:10

R Samuel Klatchko