indirect reference/use of procedures
Lynn Lively
Lynn.Lively at p4694.f506.n106.z1.fidonet.org
Tue Mar 13 07:58:16 AEST 1990
In an article of <9 Mar 90 04:51:51 GMT>, cynthia at ucselx.sdsu.edu (cynthia Anderson) writes:
cA>From: cynthia at ucselx.sdsu.edu (cynthia Anderson)
cA>Date: 9 Mar 90 04:51:51 GMT
cA>Organization: San Diego State University Computing Services
cA>Message-ID: <1990Mar9.045151.9601 at ucselx.sdsu.edu>
cA>Newsgroups: comp.lang.c
cA>
cA>hello
cA>
cA>i would like to be able to write a procedure that takes as
cA>a parameter a procedure name and then using that name
cA>calls the procedure.
cA>
cA>ie runAProcedure(myProcedure)
cA> {
cA>
cA>
cA> myProcedure
cA>
cA>
cA>}
cA>
cA>Is this possible to do in C? Any help or advise is appreciated
cA>tho please e-mail responses so the net won't be cluttered.
cA>
cA>thanks,
cA>
cA>cynthia anderson
cA><cynthia at cod.nosc.mil.uucp>
cA>or
cA><cynthia at ucselx.sdsu.edu>
cA>
cA>Disclaimer: whenever it was and whenever it happened, i'm sure i
cA>wasn't there and it couldn't have been me.
cA>t
Cynthia,
Not only is it possible it's actually fairly easy to do. You need to
declare your function in the following form.
<returntype> (* funcname) ();
Examples:
int (* myfunc)();
void (* myfunc)();
use it in the program like this
myfunc ();
If it uses parameters simply specify them at call time.
myfunc (param1, param2);
of course my the declaration is only a pointer so make sure you initialize it
to something. Hence you could do this.
int (* myprint) () = printf;
myprint ("Hello World!\n");
You can also set up tables of them if you like.
typedef INTFUNC int (* func)();
INTFUNC func_tab[10];
and later on refer to
func_tab[1]();
Hope this helps.
Your Servant,
Lynn
More information about the Comp.lang.c
mailing list