www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/22/04:10:58

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: Casting void pointers
Date: Sun, 21 Jun 1998 22:34:26 -0700
Organization: Alcyone Systems
Lines: 53
Message-ID: <358DECE1.67C137A4@alcyone.com>
References: <6mkaos$k7o AT dfw-ixnews6 DOT ix DOT netcom DOT com> <6mkfnv$hcr AT espresso DOT cafe DOT net>
NNTP-Posting-Host: kamali.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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.)

In any case, if all the original poster wanted was a "generic function
pointer" (which is how void * is being used in this case), then they can
just use some generic function pointer type -- say, int (*)(void) -- for
the conversions, and then back to the original function pointer type
before the call is made, since this guaranteed by ANSI 6.3.4.

> Yes; pointers to void may only be implicitly converted to object
> pointers
> or pointers to incomplete type. Converting a function point to void *
> and vice versa requires a cast operator.

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 */

Certainly, explicitly casting to void * is never a bad idea.  The
standard is somewhat opaque on this subject, though, so I'm not entirely
sure (ANSI 6.3.16.1)

-- 
         Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                       Alcyone Systems / http://www.alcyone.com/max/
  San Jose, California, United States / icbm:+37.20.07/-121.53.38
                                     \
                   Regret it? nope. / Said it? yep.
                                   / Ice Cube

- Raw text -


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