编译包含下面一段代码的程序,代码如下:
if (cmd == -1)
{
plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
goto SENDTCP;
}
编译结果针对这行,做出如下警告:
web_server.c(288): warning: #514-D: pointless comparison of unsigned integer with a negative constant
大意为:一个无意义的比较在无符号整型和一个负数常量
查找原因发现:发现 cmd 变量定义形式为:
unsigned char cmd;
解决办法如下:
if (cmd == (unsigned char)-1)
{
plen=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
goto SENDTCP;
}
可以思考的问题:
把 不同类型的-1 强制类型转换后的结果为多少?