why will the following error occur during compilation
As part of my program, I used following code:
///////////////
9开发者_开发百科8:::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.
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
- the
if (abc)
test is redundant (because it will never be true) or - the dereferencing of
abc
on line 98 is potentially incorrect becauseabc
might in fact be null.
If you test for abc
it means to the compiler it might be null. Therefore dereferencing the pointer like in abc->stv
is a possible error. A solution is to enclose the printf
code inside the if
block:
if(abc)
{
printf("%d",abc->stv)
...
}
精彩评论