function pointer help needed

latham at bsdpkh.UUCP latham at bsdpkh.UUCP
Fri Jan 16 17:58:48 AEST 1987


in article <1327 at loral.UUCP>, jlh at loral.UUCP (Jim Vaxkiller) says:
> Keywords: (*ptr[i])() = ???
> 
> I'm having a little problem with tables of pointers to functions.
> What I want to do is have several functions returning type void,

Try using the following .....

void err1() {}       /* NOTE : these are functions returning void !
void err2() {}
extern void err3();

static void (*ptr_tbl[])() = {
 	err1,		/* this is a table of pointers to functions     */
 	err2,		/*      that return void .......                */
 	err3		/* previously you defined them as returning int */
			/* because you only stated that they we static  */
			/* thus they returned int BY DEFAULT            */
};
 
main()
{
	(*ptr_tbl[0])();		/* invoke function */
}

A POINTER TO A FUNCTION HAS A TYPE ... it is the type that the function
returns.  If you wish to set up a table of said pointers, then you must
declare its elements as pointers to functions returning that type.

I HAVE TRIED THE ABOVE .... to avoid foot in mouth disease.  The only
reason it didn't compile was that err3 was an undefined symbol. (sysVr2.1)
************************************************************************

	"There is not now, nor will there ever be, a computer language
	 in which it is the least bit difficult to write bad programs."
					-Unknown

			Ken Latham
			uucp: ..!ihnp4!bsdpkh!latham

 P.S. The quote in the signature is not directed at the writer of the
      original article .... it is just somthing I believe.



More information about the Comp.lang.c mailing list