Re: Is this valid C statement?
From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 09/30/04
- Next message: Wojtek Lerch: "Re: contiguity of arrays"
- Previous message: Dan Pop: "Re: invalid floating point value"
- In reply to: John Harrison: "Re: Is this valid C statement?"
- Next in thread: Flash Gordon: "Re: Is this valid C statement?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Sep 2004 15:02:13 GMT
In <415bf574$1@hawk.winshop.com.au> "John Harrison" <john@hiame.com> writes:
>"Dan Pop" <Dan.Pop@cern.ch> wrote in message
>news:cj99sp$qvh$8@sunnews.cern.ch...
>>>You are right on this one. A pointer to anything can be assigned into a
>>>void *, even without a cast. ^^^^^^^^
>>
>> You are wrong on this one. Pointers to functions cannot be converted
>> (either explicitly or implicitly) to pointers to void (and vice versa).
>> Undefined behaviour due to lack of specification in the standard.
>>
>
>I can't comment as to what is/isn't in the standard, however the following
>compiles and runs (without any warnings) on every system I have:
So what? Invoking undefined behaviour doesn't require any warning.
>#include <stdio.h>
>#include <stdlib.h>
>
>typedef int (*fooptr)(int);
>
>int foo(int bar) {
> printf("foo(%d)\n", bar);
> return bar;
>}
>
>int main(void) {
> fooptr f1 = NULL;
> fooptr f2 = NULL;
> void* v1 = NULL;
>
> f1 = foo;
>
> /* cast function pointer to void* */
> v1 = (void*)f1;
>
> /* cast void* to function pointer */
> f2 = (fooptr)v1;
>
> f1(1);
> f2(2);
> return 0;
>}
>
>
>In fact, the dlopen()/dlsym() dynamic loading system used on most Unix
>systems wouldn't work if you couldn't convert a void* to a function
>pointer...
So what? This is comp.lang.c, not comp.unix.programmer. The fact that
the C standard does not define the conversion between void pointers and
function pointers doesn't mean that a Unix-specific standard cannot
define it.
Your program may not work on MS-DOS under certain memory models (16-bit
data pointers and 32-bit function pointers) and on AS/400 machines
(function pointers much wider than data pointers).
The standard provides no guarantee that void pointers are wide enough
to be able to store function pointers. In the absence of this guarantee,
your code doesn't qualify as portable.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Currently looking for a job in the European Union
- Next message: Wojtek Lerch: "Re: contiguity of arrays"
- Previous message: Dan Pop: "Re: invalid floating point value"
- In reply to: John Harrison: "Re: Is this valid C statement?"
- Next in thread: Flash Gordon: "Re: Is this valid C statement?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|