use of if (!cptr) and if (cptr), where cptr is a *

Larry Campbell campbell at redsox.bsw.com
Wed Jul 19 09:42:35 AEST 1989


We use macros called NIL and NEW:

    #define NIL(type) ((type) 0)
    #define NEW(type) ((type *) malloc(sizeof(type)))

We also use typedefs to save typing and centralize type information,
so the malloc example looks like this:

    typedef struct foo FOO;

    FOO *p;

    p = NEW(FOO);
    if (p == NIL(FOO *))
	error();

I think this makes it pleasingly clear that you're testing for an invalid
pointer, rather than the quantity zero.
-- 
Larry Campbell                          The Boston Software Works, Inc.
campbell at bsw.com                        120 Fulton Street
wjh12!redsox!campbell                   Boston, MA 02146



More information about the Comp.lang.c mailing list