[英]why will the following error occur during compilation
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
if (abc)
tests whether abc
is a null pointer or not. if (abc)
测试abc
是否为空指针。
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 either 编译器警告您,您已经假定
abc
不是空指针(通过在第98行对其进行解引用),这意味着
if (abc)
test is redundant (because it will never be true) or if (abc)
测试是多余的(因为它永远不会为真)或 abc
on line 98 is potentially incorrect because abc
might in fact be null. abc
的取消引用可能不正确,因为abc
实际上可能为null。 If you test for abc
it means to the compiler it might be null. 如果测试
abc
,则对编译器意味着它可能为null。 Therefore dereferencing the pointer like in abc->stv
is a possible error. 因此,像
abc->stv
那样取消引用指针是可能的错误。 A solution is to enclose the printf
code inside the if
block: 一种解决方案是将
printf
代码包含在if
块内:
if(abc)
{
printf("%d",abc->stv)
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.