Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1383

Re: Conversion from void* to pointer-to-function?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.std.c
Subject Re: Conversion from void* to pointer-to-function?
Date 2012-06-25 17:23 -0400
Organization A noiseless patient Spider
Message-ID <4FE8D6E4.1060608@verizon.net> (permalink)
References <ln62af6sum.fsf@nuthaus.mib.org>

Show all headers | View raw


On 06/25/2012 04:26 PM, Keith Thompson wrote:
> Consider the following toy program:
> 
> int main(void) {   
>     void *void_ptr = 0;
>     void (*func_ptr)(void) = (void(*)(void))void_ptr;
>     return 0;
> }
> 
> (Note that void_ptr is *not* a null pointer constant, it's merely a
> pointer object that happens to contain a null pointer.)
> 
> Is the conversion of void_ptr to void(*)(void) (a pointer-to-function
> type) permitted?
> 
> N1570 6.5.4 lists three constraints for the cast operator. This code
> doesn't violate any of them.
> 
> N1570 6.3.2.3 describes the semantics of conversions involving pointers.
> It mentions conversions from void* to and from pointer-to-object types,
> conversions from one pointer-to-object type to another, and conversions
> from one pointer-to-function type to another.  Conversions from void* to
> a pointer-to-function type simply aren't mentioned.

Conversion of null pointers to other pointer types is mentioned
(6.3.6.3p4). It applies only to null pointers being converted to pointer
types, but there are no other type-based restrictions on the
applicability of that section.

> My conclusion is that the conversion is permitted (it doesn't violate
> any constraint or syntax rule), but its behavior is undefined by
> omission.

The behavior is defined by 6.3.6.3p4: it produces a null pointer of type
void(*)(void).

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-06-25 13:26 -0700
  Re: Conversion from void* to pointer-to-function? James Kuyper <jameskuyper@verizon.net> - 2012-06-25 17:23 -0400
    Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-06-25 14:57 -0700
      Re: Conversion from void* to pointer-to-function? Maxim Fomin <maxim@maxim-fomin.ru> - 2012-06-27 10:03 -0700
        Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-06-27 11:33 -0700
          Re: Conversion from void* to pointer-to-function? David R Tribble <david@tribble.com> - 2012-07-24 20:05 -0700
  Re: Conversion from void* to pointer-to-function? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-21 00:32 -0800
    Re: Conversion from void* to pointer-to-function? Tijl Coosemans <tijl@coosemans.org> - 2012-12-21 10:00 +0100
    Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-12-21 02:01 -0800
      Re: Conversion from void* to pointer-to-function? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-21 08:12 -0800
        Re: Conversion from void* to pointer-to-function? Keith Thompson <kst-u@mib.org> - 2012-12-21 17:58 -0800
          Re: Conversion from void* to pointer-to-function? Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-22 08:22 -0800

csiph-web