delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/22/07:47:04

From: fred AT genesis DOT demon DOT co DOT uk (Lawrence Kirby)
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: Casting void pointers
Date: Mon, 22 Jun 98 10:08:21 GMT
Organization: none
Message-ID: <898510101snz@genesis.demon.co.uk>
References: <6mkaos$k7o AT dfw-ixnews6 DOT ix DOT netcom DOT com> <6mkfnv$hcr AT espresso DOT cafe DOT net> <358DECE1 DOT 67C137A4 AT alcyone DOT com>
Reply-To: fred AT genesis DOT demon DOT co DOT uk
Lines: 67
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <358DECE1 DOT 67C137A4 AT alcyone DOT com>
           max AT alcyone DOT com "Erik Max Francis" writes:

>Kaz Kylheku wrote:
>
>> You can create a typedef name for the function pointer and then use
>> the parenthesized typedef name. Or you can write cast expressions
>> like:
>> 
>>         void *q = 0;
>>         double (*p)(double, int) = (double (*)(double, int)) q;
>
>I think the original poster was really looking for the answer to the
>question:  For a given declaration, how do I determine its type (for
>casting)?
>
>The answer is that if you remove the identifier name from the
>declaration, that's the type.  So in the declaration int p, int is the
>type; in the declaration char *s, char * is the type, and in the
>declaration double (*p)(double, int), double (*)(double, int) is the
>type, even though it looks a little strange.
>
>> Note that conversion between pointers to void and function pointers
>> is not a feature of the C language; it is merely a common extension.
>
>Are you sure about that?  (I couldn't find a reference confirming that
>it's possible, and if there isn't one, then, well, it isn't.)

C allows you to perform the conversion usaing a cast, but it doesn't make
any guarantees about the result. There is no guarantee that void * is
capable of holding a funciton pointer (for example consider the DOS
compact(?) memory model where function pointers are 32 bits and and data
pointers including void * re only 16 bits wide).

...

>I believe implicit casts to void * (but not from void * to something
>else) are perfectly legal.  i.e., 
>
>   int i;
>   void *p = &i; /* not an error */

In C implicit conversions between void * and pointers to other incomplete
or object types are fully supported in both directions. You may be thinking
of C++.

>Certainly, explicitly casting to void * is never a bad idea.

It is a bad idea when it adds unnecessary clutter to the program.

> The
>standard is somewhat opaque on this subject, though, so I'm not entirely
>sure (ANSI 6.3.16.1)

The constraints of 6.3.16.1 state explicitly that this is allowed:

"- one operand is a pointer to an object or incomplete type and the other
   is a pointer to a qualified or unqualified version of void, and the type
   pointed to by the left has all the qualifiers of the type pointed to by
   the right;"

-- 
-----------------------------------------
Lawrence Kirby | fred AT genesis DOT demon DOT co DOT uk
Wilts, England | 70734 DOT 126 AT compuserve DOT com
-----------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019