警告:在 C++ 中比较数组和字符串

warning: in c++ compare array and string

本文关键字:数组 字符串 比较 C++ 警告      更新时间:2023-10-16

我有这个警告:

警告:多字符字符常量 [-Wmultichar]

被告是:

if (strchr(test[0],'E3')){

如果我编辑

 if (strchr(test[0],"E1")){

我有这个错误:

错误:从"常量字符*"到"整数"的转换无效 [-允许]

考虑测试[0]声明为:

 char *test[10];

我该如何解决?

为什么不使用 std::string::find?

std::string str(test[0]);
auto idx = str.find("E3");

这是函数原型:

const char * strchr ( const char * str, int character );

strchr() 函数返回指向第一次出现的指针字符串 S 中的字符 C。

您对函数原型的使用是错误的。

 if (strchr(test[0],"E1")){

因此,编译器出错。

''将为您提供要搜索的字符的 ASCII 值。

  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in "%s"...n",str);
  pch=strchr(str,'s');