Topic: char c = NULL;


Author: pkt@lpi.liant.com (Scott Turner)
Date: Wed, 13 Oct 1993 22:03:14 GMT
Raw View
Recent discussion in comp.lang.c++ indicates that many programmers
use NULL as a null pointer constant because it shows their intent more
specifically than would 0.

Fergus Henderson has proposed an interesting implementation of NULL
which supports this usage.  The NULL macro expands to a keyword, as in:

 #define NULL __builtin_null_pointer

so that NULL can be used only as a null pointer constant.  This
implementation catches

    char c = NULL;  // error
    foo(NULL);      // error if foo does not accept a pointer argument

The standards committee's working paper is not clear about the legality
of this implementation.  So I'll raise the issue.  Should
    char c = NULL;

    1. violate a constraint which implementations must diagnose
    2. be implementation-dependent (optionally diagnosed)
or  3. be valid C++?

1 and 2 have the advantage of diagnosing a coding error.

3 has the advantage of not breaking existing code (but note
that such code is certainly dubious, and that this change does
not quietly alter its meaning).

1 has the drawback of imposing implementation cost.

2 has the drawback that implementations will vary, making porting a
little harder.

----

In case you don't have the working paper, in 4.6 (Pointer Conversions)
it defines "null pointer constant" in the following sentence.

    A constant expression (5.19) that evaluates to zero (the null
    pointer constant) when assigned to, compared with, alternated
    with (5.16), or used as an initializer of an operand of pointer
    type is converted to a pointer of that type.

The working document for the standard library requires NULL to expand
to a C++ null pointer constant.

----

Personally, I prefer to use NULL rather than 0, and hope that this
diagnostic can and will be implemented, but I don't feel like forcing
it on implementers.

--
Prescott K. Turner, Jr.
Liant Software Corp. (developers of LPI languages)
959 Concord St., Framingham, MA 01701 USA    (508) 872-8700
UUCP: uunet!lpi!pkt                          Internet: pkt@lpi.liant.com