为什么编译时会出现如下错误
作为我的程序的一部分,我使用了以下代码:(
///////////////
98:::printf("%d",abc->stv)
//////////////
100::if(abc)
//////////////
产生了以下错误)
Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 100
As part of my program, I used following code:
///////////////
98:::printf("%d",abc->stv)
//////////////
100::if(abc)
//////////////
(the following error was produced)
Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 100
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
if (abc)
测试abc
是否为空指针。编译器警告您,您已经假设
abc
不是空指针(通过在第 98 行取消引用它),这意味着if (abc)
测试是多余的(因为它永远不会为真),或者abc
的取消引用可能是不正确的,因为abc
实际上可能为 null。if (abc)
tests whetherabc
is a null pointer or not.The compiler is warning you that you've already assumed that
abc
is not a null pointer (by dereferencing it on line 98), which means eitherif (abc)
test is redundant (because it will never be true) orabc
on line 98 is potentially incorrect becauseabc
might in fact be null.如果您测试
abc
,则对编译器来说意味着它可能为 null。因此像abc->stv
那样取消引用指针是一个可能的错误。解决方案是将printf
代码包含在if
块内:If you test for
abc
it means to the compiler it might be null. Therefore dereferencing the pointer like inabc->stv
is a possible error. A solution is to enclose theprintf
code inside theif
block: