Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!cmcl2!husc6!mit-eddie!genrad!decvax!savax!elrond!amamaral
From: amamaral@elrond.UUCP (Alan Amaral)
Newsgroups: comp.lang.c
Subject: Re: function pointer help needed
Message-ID: <592@elrond.UUCP>
Date: Mon, 12-Jan-87 13:03:32 EST
Article-I.D.: elrond.592
Posted: Mon Jan 12 13:03:32 1987
Date-Received: Tue, 13-Jan-87 01:24:37 EST
References: <1327@loral.UUCP>
Organization: Calcomp Display Products Division, Hudson, NH, USA
Lines: 131
Keywords: (*ptr[i])() = ???
Summary: Try one of these methods.

In article <1327@loral.UUCP>, jlh@loral.UUCP (Jim Vaxkiller) writes:
> I'm having a little problem with tables of pointers to functions.
> What I want to do is have several functions returning type void,
> then put their addresses into a table and execute the function by
> indexing into the table.  Assembly language programmers out there
> will recognize this as a simple jump table.  Now if I have my functions
> return ints I'm fat, dumb, and happy.  It's when I try to use functions
> returning voids that the compiler burps.  I know about the void problem
> in the 4.2BSD compiler and have typedef'd voids to ints, but to no avail.
> The following code fragment illustrates my problem.
> 
> 
> void				/* 2 do nothing routines returning */
> err0()				/*    different data types */
> {}
> 
> int
> err1()
> {}
> 
> static (*ptr_tbl[])() = {	/* table of 2 function pointers */
> 	err0,			/* this is what the compiler hates */
> 	err1
> };
> 
> main()
> {
> 	(*ptr_tbl[0])();		/* invoke function */
> }
> 
> 
> Granted, I could have my functions all return ints, but I don't want
> to do that because they don't return anything and lint complains.
> My system is a vax 11/750 running 4.2BSD UN*X.  Any suggestions on
> how to make this work would be much appreciated.
> 
> 
> 					Jim
> 
> Jim Harkins 
> Loral Instrumentation, San Diego
> {ucbvax, ittvax!dcdwest, akgua, decvax, ihnp4}!sdcsvax!sdcc6!loral!jlh


Here is one way:

void				/* 2 do nothing routines returning */
err0()				/*    different data types */
{printf("testing 123\n");}

int
err1()
{}

void (*ptr_tbl[2])();		/* table of 2 function pointers */

void init()
{
    ptr_tbl[0] = err0;
}

main()
{
	init();
	(*ptr_tbl[0])();		/* invoke function */
}


Here is another way:

int				/* 2 do nothing routines returning */
err0()				/*    different data types */
{}

int
err1()
{}

static (*ptr_tbl[])() = {	/* table of 2 function pointers */
	err0,			/* this is what the compiler hates */
	err1
};

main()
{
	(void) (*ptr_tbl[0])();		/* invoke function */
}


Neither causes complaints from cc or lint (except for the unused err1 in
the first example that is).  I have found that this method is VERY nice
for doing lots of different things cleanly.  For example, I have a Ray
Tracing program that uses this sort of mechanism all over the place for
keeping methods straight for each object that I am rendering.  For
example:

	/* intersect ray with all known objects */
	for(i=0; i