[英]How do you use a string array in a conditional statement in C?
I want to use a string array in an if statement to test whether the input string matches any of the strings in the array. 我想在if语句中使用字符串数组来测试输入字符串是否与数组中的任何字符串匹配。
So far this is what I've tried: 到目前为止,这是我尝试过的:
void checkForError(char input[50])
{
const char *input2[]={"print","loadStarter","terminate()"};
if(input != input2)
{
printf("Error:Incorrect method '%s'.\n",input);
}
else
{
abort();
}
}
And if I were to enter something in the array like "print" it would end up showing me: 如果我要在数组中输入“ print”之类的内容,它将最终向我显示:
Error:Incorrect method 'print'. 错误:方法“打印”不正确。
but when I try something not listed in the array like "g" it repeats the error message nonstop. 但是,当我尝试未在数组中列出的诸如“ g”之类的内容时,它会不停地重复错误消息。
I was thinking perhaps something like this could work: 我在想也许这样的事情可能会起作用:
void checkForError(char input)
{
if(strcmp(input,"print"))!=0 || strcmp(input,"loadStarter"))!=0 || strcmp(input,"terminate()")
{
printf("Error:Incorrect method '%s'.\n");
}
else
{
abort();
}
}
But it turns out that actually doesn't work so what do I do? 但是事实证明实际上是行不通的,那我该怎么办?
You cannot compare strings (usefully) in C using ==
and !=
; 您不能使用==
和!=
比较C中的字符串(有用); you must use library functions such as strcmp
instead. 您必须改用诸如strcmp
库函数。 See How to compare strings in an "if" statement? 请参阅如何比较“ if”语句中的字符串? for details. 有关详细信息。
I think a good solution to your question would be to loop around your array, aborting on the first match. 我认为,对您的问题的一种好的解决方案是在数组周围循环,在第一个匹配项时中止。
void checkForError(char* input)
{
const char *input2[]={"print","loadStarter","terminate()"};
const int len = sizeof(input2)/sizeof(input2[0]);
for (int i = 0; i < len ;i ++)
{
if (strcmp(input,input2[i]) == 0)
{
//I have matched the string input2[i]
abort();
}
}
// Nothing matched
printf("Not found\n");
}
This would also be easier to extend than any handcoded method. 这也比任何手工编码的方法都容易扩展。
Also, if you plan on doing a lot with these strings, and you have a limited number of strings, you should probably turn them into some sort of enum. 另外,如果您打算对这些字符串做很多事情,并且字符串数量有限,则可能应该将它们变成某种枚举。 This way you do not have to have strcmp scattered everywhere, and you can use a standard switch statement. 这样,您不必将strcmp分散在各处,并且可以使用标准的switch语句。
Your second thought is correct, you should not compare the strings using ==
operator, anyway, I'm not sure whether the rest is a typo or not, but it should be like that: 您的第二个想法是正确的,无论如何,我不应该使用==
运算符比较字符串,我不确定其余的是否是错字,但应该是这样的:
void checkForError(char * input)
// ^ note the * (pointer)
{
if(strcmp(input,"print")!=0 || strcmp(input,"loadStarter")!=0 || strcmp(input,"terminate()") != 0)
// you forgot brackets
{
printf("Error:Incorrect method '%s'.\n", input);
// ^ you forgot the "input" parameter
}
else
{
abort();
}
}
a better method would be to have a return value then have your error message depend on return value. 更好的方法是有一个返回值,然后让您的错误消息取决于返回值。
// return 1 when string is OK, 0 otherwise:
int checkForError(const char* input)
{
if(!strcmp(input,"print")) || !strcmp(input,"loadStarter")0 || !strcmp(input,"terminate()")
{
return 1;
}
else
{
return 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.