delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/03/10:00:32

From: Stefan Wiermann <wiers000 AT mail DOT Uni-Mainz DOT DE>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: function pointers
Date: Mon, 3 Jul 2000 15:56:08 +0200
Organization: Johannes Gutenberg-Universitaet Mainz, Germany
Lines: 76
Message-ID: <Pine.OSF.4.21.0007031528170.32646-100000@omalley.zdv.Uni-Mainz.DE>
References: <8mf0msokv9ejp5c0b3pv0dhmaqmidgps6v AT 4ax DOT com>
NNTP-Posting-Host: omalley.zdv.uni-mainz.de
Mime-Version: 1.0
X-Trace: bambi.zdv.Uni-Mainz.DE 962632571 26529 134.93.8.121 (3 Jul 2000 13:56:11 GMT)
X-Complaints-To: usenet AT mail DOT uni-mainz DOT de
NNTP-Posting-Date: 3 Jul 2000 13:56:11 GMT
X-Sender: wiers000 AT omalley DOT zdv DOT Uni-Mainz DOT DE
In-Reply-To: <8mf0msokv9ejp5c0b3pv0dhmaqmidgps6v@4ax.com>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Mon, 3 Jul 2000, Sean Proctor wrote:

o> okay, I posted this on comp.lang.os  I don't think there is a way to
> do this well... what I'd basically like to do is have a pointer to a
> function, and a pointer to an argument list which I can put anything
> into...  I'd like to call library functions, but I don't know which
> I'm going to call, or what the arguments are going to be at
> compilation.  here's what I'd like to do:
> 
> void *args;
> int (*general_fun)();
> int fun1(int);
> int fun2(float, int);

You may not pass different types/argument lists to a (*myfunc) () call.
You declared (*general_fun)() implicitely as (*general_fun)(void).
So you are allowed to call it without varlist only.

My first suggestion: If you want to pass only a limited type of different
var lists like here either int or float,int, pass it as structure:

typedef struct mystruct <brace> float foo; int bar; <brace>;

or (better) as pointer to mystruct: (*general_fun)(mystruct*)....
May be you want to have a union instead of a struct.
But in both cases you have to provide information how the varlist is to be
read:
e.g 

---SNIP---

struct bla <brace> count_f; // number of passed floats 
                   count_i; // number of passed ints
	           float *float_pointer //points to an array of floats
	           int   *int_pointer   // the same for ints
	   <brace> 

---SNIP---

Alternatively, you can use the stdarg-lib which provides handling of
variable var lists.

Me, personally, I prefer the first method, because I can not help, the
stdarg-thingies look kinda weird and tricky ;-)


> 
> int main(void)
> {
> 	int test, fun1_arg1, fun2_arg2;
> 	float fun2_arg1;
> 	...
> 	if(test) {
> 		general_fun = fun1;
> 		args = fun1_arg1;
> 	}
> 	else {
> 		general_fun = fun2;
> 		args = malloc(sieof(float) + sizeof(int));
> 		*args = fun2_arg1;
> 		*(args + sizeof(float)) = fun2_arg2;
> 		/* I know the above 3 lines don't work at all, but I'm
> hoping someone will understand what I hope to accomplish by them and
> suggest a solution */

-----SNIP----

Uhoh, I think it's clear what <brace> means. My stone-aged MWM provides
some very weird xterm restrictions.

WBR

Stefan Wiermann



- Raw text -


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