我在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/