我正在研究 CS50 习题集。在此函数中,我迭代一行以确保它符合某些规则:“方法 SP 请求-目标 SP HTTP-版本 CRLF”,其中 SP 是空格,CRLF 是回车/换行符。
我通过字符串的最后一部分来查找 CRLF 以确认它的存在,方法如下:
//needle2 is a subset of the line, here it's the last bit: "HTTP-version CRLF"
const char* needle3 = strchr(needle2, '\r\n');
if (needle3 == NULL)
{
error(400);
return false;
}
编译此代码时收到错误消息:错误:
error: multi-character character constant [-Werror,-Wmultichar]
const char* needle3 = strchr(needle2, '\r\n');
据我所知,我正在一个函数中寻找多个字符,而该函数一次只能接收 1 个字符。 但是我如何查找 CRLF 以确保它存在而不出现多字符错误?
我尝试以与分发代码使用它完全相同的方式使用 strstr() 函数,但出现错误,这更加令人困惑,因为我使用在同一程序中工作的代码。
最佳答案
正如保罗·奥格维建议的那样:
//needle2 is a subset of the line, here it's the last bit: "HTTP-version CRLF"
const char* needle3 = strstr(needle2, "\r\n");
if (needle3 == NULL)
{
error(400);
return false;
}
关于c - strchr()中多字符字符常量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204312/