Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!texbell!merch!sneaky!gordon From: gordon@sneaky.UUCP (Gordon Burditt) Newsgroups: comp.lang.c Subject: Re: Passing functions in C Message-ID: <29381@sneaky.UUCP> Date: 15 Mar 90 06:40:14 GMT References: <1990Mar9.045151.9601@ucselx.sdsu.edu> <3242@hcx1.SSD.CSD.HARRIS.COM> Distribution: usa Organization: Gordon Burditt Lines: 49 >>i would like to be able to write a procedure that takes as >>a parameter a procedure name and then using that name >>calls the procedure. >> >>ie runAProcedure(myProcedure) >> { >> myProcedure >>} >> >>Is this possible to do in C? Any help or advise is appreciated >>tho please e-mail responses so the net won't be cluttered. > >NO! It is not possible to do in C! (Im pretty sure you cant do this >in ANSI C either). Yes, it is. Some really old compilers did accept myfunct(), where myfunct is of type pointer-to-function-returning-something, as meaning the same as (*myfunct)(). Modern compilers tend to at least give warnings about it. From section 3.7.1 of the Jan 1988 ANSI C draft (sorry, that's the most recent one I have): QUOTE To pass one function to another, one might say int f(void) /* ... */ g(f); Note that f must be declared explicitly in the calling function, as its appearance in the expression g(f) was not followed by (. Then the definition of g might read g(int (*funcp)(void)) { /* ... */ (*funcp)() /* or funcp() ... */ } or, equivalently, g(int func(void)) { /* ... */ func() /* or (*func)() ... */ } END QUOTE. ANSI allows this, and even gives an example of it. Gordon L. Burditt sneaky.lonestar.org!gordon