c - strcasecmp NULL 参数; man strcasecmp

标签 c string

我在strcasecmp上做了一个sigsegved的小程序,直到我做了这个测试用例才知道为什么:

strcasecmp(NULL, "TEST"); 

编译时,它给了我以下警告:
test.c:9:4: warning: null argument where non-null required (argument 1) [-Wnonnull]

然而,man strcasecmp没有说 NULL争论,有人可以解释一下我如何从理论上从阅读文档中推断出这一点,而不是凭经验编写测试用例?它是根深蒂固的标准吗?或者 const char *无权成为NULL ,出于某种原因,我不知道?

最佳答案

没有人真正解释过为什么会这样。

它是未定义的行为。为什么?

Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.



(C 标准,7.1.4)

由于 strcasecmp 从未提及 NULL,因此它在函数的域之外。因此,该功能可以随心所欲。包括崩溃。

(注意:我的来源是这个相关的答案:https://stackoverflow.com/a/12621203/1180785)

关于c - strcasecmp NULL 参数; man strcasecmp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18352792/

相关文章:

c - C 中奇怪的段错误

c++ - 是否允许 std::string 的 end+1 迭代器?

javascript - 如何使用正则表达式在字符串中强制使用字符/特殊字符

ios - 如何将json响应数据分解为多个字符串?

c - 这是一个 C 程序,用于查找具有相同数字的下一个最大数字。但是没有通过一个测试用例

c - C 中的归并排序和快速排序

CFString内存泄漏

Java字符串到不同格式的日期转换

比较两个字符串并查找不匹配计数

c - C 是否在内部使用 Karatsuba 算法将两个整数相乘?