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


Groups > comp.std.c > #1382

Conversion from void* to pointer-to-function?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!204.52.135.9.MISMATCH!newsfeed.hal-mli.net!feeder1.hal-mli.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Conversion from void* to pointer-to-function?
Date Mon, 25 Jun 2012 13:26:25 -0700
Organization None to speak of
Lines 43
Message-ID <ln62af6sum.fsf@nuthaus.mib.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info mx04.eternal-september.org; posting-host="WEKlRqLUVZ7RHprEch2nAg"; logging-data="31636"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WtEtsdb4YkvNDXOKKCBDE"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock sha1:nQ0IFZhJTfxqSCFAj3lDs2A4Yzs= sha1:oEt3KY4+Wxage6iHsEXW9Ou7ldM=
Xref csiph.com comp.std.c:1382

Show key headers only | View raw


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.

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

Note that the authors of gcc seem to have a different interpretation;
on the above code, gcc-4.7 with "-std=c99 -pedantic" produces the
following warning:

    warning: ISO C forbids conversion of object pointer to function pointer type [-pedantic]

Frankly I think it would make more sense if the conversion *were*
a constraint violation, or else if its behavior were explicitly
stated to be undefined (or the result to be implementation-defined
or unspecified).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.std.c | Previous | Next — 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