delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/04/17:30:15

From: Sean Proctor <sproctor AT ccs DOT neu DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: function pointers
Message-ID: <56k4msgsfdji2io0mvelg7akfquk6eg5m4@4ax.com>
References: <8mf0msokv9ejp5c0b3pv0dhmaqmidgps6v AT 4ax DOT com> <Pine DOT OSF DOT 4 DOT 21 DOT 0007031528170 DOT 32646-100000 AT omalley DOT zdv DOT Uni-Mainz DOT DE>
X-Newsreader: Forte Agent 1.7/32.534
MIME-Version: 1.0
Lines: 83
Date: Tue, 04 Jul 2000 20:57:30 GMT
NNTP-Posting-Host: 207.16.153.219
X-Complaints-To: Abuse Role <abuse AT enter DOT net>, We Care <abuse AT newsread DOT com>
X-Trace: monger.newsread.com 962744250 207.16.153.219 (Tue, 04 Jul 2000 16:57:30 EDT)
NNTP-Posting-Date: Tue, 04 Jul 2000 16:57:30 EDT
Organization: ENTER.net (enter.net)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Mon, 3 Jul 2000 15:56:08 +0200, Stefan Wiermann
<wiers000 AT mail DOT Uni-Mainz DOT DE> wrote:

>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
>
>
as I said, I can't do that since they're library functions.  I'm just
gonna make wrapper functions cause that's the only thing I can think
of.

Sean

- Raw text -


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