🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3101

Re: gcc and NULL function pointers.

#3101
From: Norman Diamond
Date: Tue, 04 Jun 1991 01:29
39 lines
2942 bytes
In article <16386@ganymede.inmos.co.uk> conor@inmos.co.uk () writes:
>  void (*ptr)(void); /* pointer to void function returning void */
>  ptr = (void *)0;                   /* 2 */
>  ptr = (void (*)(void))((void *)0); /* 4 */
>When compiled with gcc version 1.39, with -ansi -pedantic,
>it reports a warning on the line marked /* 2 */:
>c.c:6: warning: assignment between incompatible pointer types
>
>ANSI standard states (section 3.3.16.1) that a constraint on simple assignment
>"the left operand is a pointer and the right is a null pointer constant".
>The index points me to section 3.2.2.3 for a definition:
>"An integral constant expression with the value 0, or such an expression
>cast to type void *, is called a null pointer constant.
>If a null pointer constant is assigned to or compared for equality to a
>pointer, the constant is converted to a pointer of that type"
>Thus, I claim that /* 2 */ should be converted by the compiler to /* 4 */
>and thus should not create a warning. Hence gcc is incorrect in this point.

I agree.  A null pointer constant of type (void *) has a certain privilege
which is not inherited from either of its parent categories; it is compatible
with function pointer types as well as with object pointer types.

(Non-constant null pointers of type (void *) are compatible only with object
pointer types, and constant non-null pointers of type (void *) are also
compatible only with object pointer types.)

Now to be pedantic, my statement might be too broad.  If an integral constant
expression with value 0 is cast to type (void *), it is a null pointer
constant.  But if a null pointer constant is cast to type (void *), is it still
a null pointer constant?  It is still constant (I think) and is still a null
pointer, but does it still have its extra privilege?  The standard doesn't say!
  ptr = (void *)0;          /* Conversion to type of function pointer, yes */
  ptr = (void *)(void*)0;   /* Not defined! */
>On our system, NULL is #defined in a header file to be ((void *)0),
So, be careful casting your uses of the NULL macro!!
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Permission is granted to feel this signature, but not to look at it.
Technical Details
Message-ID: <1991Jun4.012914.25418@tkou02.enet.dec.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!pa.dec.com!jrdzzz.jrd.dec.com!tkou02.enet.dec.com!jit533!diamond
References: <16386@ganymede.inmos.co.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!pa.dec.com!jrdzzz.jrd.dec.com!tkou02.enet.dec.com!jit533!diamond
From: diamond@jit533.swstokyo.dec.com (Norman Diamond)
Newsgroups: comp.std.c
Subject: Re: gcc and NULL function pointers.
Message-ID: <1991Jun4.012914.25418@tkou02.enet.dec.com>
Date: 4 Jun 91 01:29:14 GMT
References: <16386@ganymede.inmos.co.uk>
Sender: usenet@tkou02.enet.dec.com (USENET News System)
Reply-To: diamond@jit533.enet@tkou02.enet.dec.com (Norman Diamond)
Organization: Digital Equipment Corporation Japan , Tokyo
Lines: 39