Re: Is this valid C statement?

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 09/30/04


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


Relevant Pages

  • Re: ptr conversions and values
    ... That's why the standard explicitly ... > representation, without providing an explanation of what pairs of values are ... So I have given an explanation how to connect integers to pointers ... > won't have any more or any less issues than conversion does. ...
    (comp.std.c)
  • Re: "<>", a relational operator?
    ... And in Standard C there are significant restrictions ... OTOH in BCPL and B pointers were ... has no whole array operations; if you want something done to all (or ... the Standard isn't vague at all -- this is specifically ...
    (comp.lang.fortran)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... The standard form is like this, ... 1108 The sizeof operator ... ... being able to compare unrelated pointers, ... So long as the pointers are of the same type, ...
    (comp.lang.c)
  • Re: bug in visual studio .net 2003 - breakpoints and memcpy
    ... Posix is a standard too with no less (some would say ... > platforms that support the conversion. ... Conversion between code pointers to integers does not exist for the sole ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to convert a vector type into string?
    ... "A superset of the tests available with other libraries, this feature helps you detect invalid iterator operations in the Standard Template Library, as well as null pointers and invalid ordering rules for sorting ... It seems only natural to me that a function might check its preconditions, or might not, or might crash, if you pass the wrong arguments. ...
    (microsoft.public.vc.language)