在C ++中比较字符数组和字符串文字

时间:2022-02-24 09:37:41

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.

这不会产生任何编译器错误,但它的行为不符合预期。

3 个解决方案

#1


17  

std::strcmp returns 0 if strings are equal.

如果字符串相等,std :: strcmp返回0。

#2


24  

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

查看strcmp的文档。提示:它不返回布尔值。

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.

ETA:==通常不起作用,因为cstr1 == cstr2比较指针,因此只有当cstr1和cstr2指向相同的内存位置时,才会进行比较,即使它们恰好都引用了字典相等的字符串。您尝试过的(将cstring与文字进行比较,例如cstr ==“是”)尤其不起作用,因为标准并不要求它。在一个合理的实现中我怀疑它会爆炸,但cstr ==“yes”不太可能成功,因为cstr不太可能引用字符串常量“yes”所在的地址。

#3


3  

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

strcmp返回一个三态值,以指示两个字符串的相对顺序。当进行类似strcmp(a,b)的调用时,函数返回

  • a value < 0 when a < b
  • a 时的值

  • 0 when a == b
  • 当a == b时为0

  • a value > 0 when a > b
  • 当a> b时,值> 0

#1


17  

std::strcmp returns 0 if strings are equal.

如果字符串相等,std :: strcmp返回0。

#2


24  

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

查看strcmp的文档。提示:它不返回布尔值。

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.

ETA:==通常不起作用,因为cstr1 == cstr2比较指针,因此只有当cstr1和cstr2指向相同的内存位置时,才会进行比较,即使它们恰好都引用了字典相等的字符串。您尝试过的(将cstring与文字进行比较,例如cstr ==“是”)尤其不起作用,因为标准并不要求它。在一个合理的实现中我怀疑它会爆炸,但cstr ==“yes”不太可能成功,因为cstr不太可能引用字符串常量“yes”所在的地址。

#3


3  

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

strcmp返回一个三态值,以指示两个字符串的相对顺序。当进行类似strcmp(a,b)的调用时,函数返回

  • a value < 0 when a < b
  • a 时的值

  • 0 when a == b
  • 当a == b时为0

  • a value > 0 when a > b
  • 当a> b时,值> 0