Re: Style question: Should one check for NULL pointers?

David D. Hagood
2003-07-11T13:58:29Z

Hua Zhong wrote:

> Not always true. In some cases you know how to handle: just return
> without doing anyting.

That is NOT an error condition - the API specifically allows NULL to be
passed in, and specifically states that no action will be taken in that
case.

But consider the following code:

sscanf(0,0);

That IS an error condition - both the string to scan and the format
string are NULL. In this case sscanf should return EITHER 0 (no items
matched) or better still -1 (error).

As others have said - ideally, if you have any doubt about a new
function you are writing being able to succeed, you should write it to
return a success report.

However, my whole point was that simply checking for null and doing
nothing when null was not a valid value was violating the rule of "don't
check if you don't know how to handle".