www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/07/03/04:59:19

From: Sean Proctor <sproctor AT ccs DOT neu DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: function pointers
Message-ID: <8mf0msokv9ejp5c0b3pv0dhmaqmidgps6v@4ax.com>
X-Newsreader: Forte Agent 1.7/32.534
MIME-Version: 1.0
Lines: 117
Date: Mon, 03 Jul 2000 07:44:04 GMT
NNTP-Posting-Host: 207.16.154.90
X-Complaints-To: Abuse Role <abuse AT enter DOT net>, We Care <abuse AT newsread DOT com>
X-Trace: monger.newsread.com 962610244 207.16.154.90 (Mon, 03 Jul 2000 03:44:04 EDT)
NNTP-Posting-Date: Mon, 03 Jul 2000 03:44:04 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

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

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

	(*general_fun)(*args);
	...
}

onto djgpp specific things... here's my segment of code that produces
an internal compiler error.  this shouldn't compile, and I think
anyone who gets this error should know that they messed up, but I'll
post it anyway.

#include <stdio.h>

int (*gen_fun)();
int fun1(int);
int fun2(int, int);
void *args;

int main(void)
{
	int test = 1;
	int a, b;
	a = 5;
	b = 7;
	if(test) {
		gen_fun = fun1;
		args = a;
	}
	(*gen_fun)(*((int *)args));
	return 0;
}

and using RHIDE it gives...

Internal compiler error in 'do_spec_1' at
/djgpp/gnu/gcc-2.952/gcc/gcc.c:3794

I'm guessing it doesn't like me doing funky things to an empty
parameter list... if anyone has an effecient way of doing what I want.
please let me know, I'd rather not have to pass it off to another
function to simply re-direct to the function I want... like:

typedef int GENERIC_FUN(void *args);
GENERIC_FUN fun1
{
	return some_lib_function((FUN1_ARGS *)args->a); /*if these
even works */
}

GENERIC_FUN fun2
{
	return some_other_lib_function((FUN2_ARGS *)args->b,
((FUN2_ARGS * )args->c);
}

typedef struct FUN1_ARGS {
	int a;
} FUN1_ARGS:

typedef struct FUN2_ARGS {
	float b;
	float c;
}

int main(void)
{
	void *arg;
	FUN1_ARGS fun1_args = {0};
	FUN2_ARGS fun2_args = {0.0, 0.0};
	GENERAL_FUN *fun_ptr;
	int test;
	...
	if(test) {
		fun_ptr = fun1;
		arg = &fun1_args;
	}
	else {
		fun_ptr = fun2;
		arg = &fun2_args;
	}
	(*fun_ptr)(arg);
	...
}

I'm open to any suggestions

Thanks.
Sean

- Raw text -


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