Pointer if test for for NULL

Paul Whitfield paulw at omnitronics.com.au
Thu Apr 29 02:08:06 UTC 2010


On 29/04/2010 9:39 AM, Chris Johns wrote:
> Hello,
>
> The Austin Group (Open Group) has been having a discussion about NULL
> meaning all bits 0 [0][1]. For example:
>
> struct s* p;
> p = malloc (sizeof (struct s));
> if (p)
> memset (p, 0, sizeof (*p));
>
> The issue raised is "If 'struct s' contains a pointer is the pointer
> initialised to NULL correctly ?". This is not what I wish to discuss.
>
> Considering this I am wondering if the 'if (p)' code is strictly valid ?
>
> Does this code also imply false happens if NULL is all bits 0 ?
>
> Should this code be changed to:
>
> if (p != NULL)
>
> I am guilty of coding the if's this way and I am wondering if this
> should be changed.
>

According to the C-Faq it is *always* valid

http://c-faq.com/null/ptrtest.html

The article quotes various standards / page numbers

Regards

Paul Whitfield.
-- 
Message  protected by MailGuard: e-mail anti-virus, anti-spam and content filtering.http://www.mailguard.com.au/mg




More information about the users mailing list