检查最小值:
#include <stdio.h>
#include <string.h>
int main(void) {
char newline = '\n';
char* p = &newline;
if(strcmp(p, "\n") == 0) {
printf("ok\n");
} else {
printf("wrong\n");
}
return 0;
}
这是未定义的行为吗?或者它只是错误的(即它总是返回不相等)?不管是什么,请解释原因!
最佳答案
它是 UB 的原因很简单,p
不是空终止字符串,如果输入非空终止字符串,strcmp
是 UB。
关于c - 当 p 指向一个字符时,strcmp(p, "\n") 是未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33084153/