Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!oberon!panarea.usc.edu!english
From: english@panarea.usc.edu (Joe English)
Newsgroups: comp.lang.c
Subject: Re: An array of function pointers.
Message-ID: <12299@oberon.USC.EDU>
Date: 21 Sep 88 23:55:06 GMT
References: <414@ucrmath.UUCP>
Sender: news@oberon.USC.EDU
Reply-To: english@stromboli.usc.edu (Joe English)
Organization: University of Southern California
Lines: 33

In article <414@ucrmath.UUCP> jantypas@ucrmath.UUCP (John Antypas) writes:
>We'd like to set up something like a vector table.
>If the vector table has a NULL in a spot, then the interpreter will use its
>internal routines instead of faster device specific code.  As an example:
>
>vectors[] =	{ dev_line(),
>		  dev_box(),
>		  dev_circle(),
>		  dev_arc(),
>		  dev_text()
>		}
>
>[...] ... when I 
>try to copy function addresses, the compiler complains.  How do I get the
>address of a funciton and put it into an array for calling later?

This works, (and it's an excellent way to set up a device driver, I
might add.)  The only problem with the initialization is that you're
using function _call_ syntax, which can't be evaluated at compile
time.  If you declare the functions (int dev_line(), dev_box(),...;)
beforehand, you can write:

int (*vectors)()[] = { dev_line, 
                       dev_box,
                       ...
                     }

_without_ parentheses; this evaluates to the address of the function,
which can be initialized, instead of a function call.

      /|/| Joe English
-----< | | ARPA: english%lipari@oberon.usc.edu
  O   \|\|