delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/23/03:15:49

From: Vik Heyndrickx <Vik DOT Heyndrickx AT rug DOT ac DOT be>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: Casting void pointers
Date: Tue, 23 Jun 1998 09:06:38 +0200
Organization: University of Ghent, Belgium
Lines: 36
Message-ID: <358F53FE.4D97@rug.ac.be>
References: <6mkaos$k7o AT dfw-ixnews6 DOT ix DOT netcom DOT com>
NNTP-Posting-Host: eduserv1.rug.ac.be
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Todd Rowan wrote:
> 
> How do you explicity cast a void pointer to a function pointer? Or do you
> even need to cast void pointers manually?

To be able to cast a function pointer to a void pointer is a DJGPP
extension to the C language. It is not standard available in all C
implementations or on all platforms. Please consider that when using it!

  void fn(double,double); // example: fn's prototype
  void *vp;

  vp = (void*)fn; // On older C implementations (pre-ANSI) you had to
write (void*)&fn

Now the answer to the question (note that each brace is required):

  fn's type is ((void*)(double,double))

So casting vp to a function pointer of type ((void*)(double,double))
gives:
  
  ((void*)(double,double))vp

When you want to call this function with a number of arguments, you have
to write e.g.:

  (((void*)(double,double))vp)(3.0,-2.0);

Note that the excessive amount of braces is required.
Note also that I haven't tested any of this, but it should work.

-- 
 \ Vik /-_-_-_-_-_-_/
  \___/ Heyndrickx /
   \ /-_-_-_-_-_-_/  Knight in the Order of the Unsigned Types

- Raw text -


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