Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: address of function Message-ID: <312@hadron.UUCP> Date: Mon, 17-Mar-86 20:56:32 EST Article-I.D.: hadron.312 Posted: Mon Mar 17 20:56:32 1986 Date-Received: Fri, 21-Mar-86 05:23:50 EST References: <2287@amd.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 40 Keywords: address of function Summary: Declare it, then use it. In article <2287@amd.UUCP> shankar@amd.UUCP (Shankar--Hq Apps) writes: > ... I would like to know how to find the address of a >function in C. Simplicity itself. First, you must make sure that it is declared in the context in which you want to use it. (I like to do that at the function-declaration level, but that's personal preference: I have also done it at the module (file), the include-file, and even the block level.) After that, any mention you make of the function, besides calling it, will use the pointer. You see, the alternative is to use the object itself; but the object in this case is the body of the function itself -- a little too large too handle easily. int example(x) int x; { int (*fptr)(); /* A pointer declared to hold a fn */ int cracker_jack(); /* Declares a function. */ /* ** My convention is to use "extern" only for functions that ** are outside this module, as: */ extern int istty(); fptr = cracker_jack; /* Not a call: coerced to pointer-fn */ cracker_jack(); /* This is a call, obviously. */ (*fptr)(); /* So is this. */ fptr(); /* Magic! So is this. [No flames.] */ /* ** Note that the last two would have been function calls even ** before the assignment 6 lines back; but before the assign- ** ment, the pointer held garbage; and so your program would ** have been trashed. */ return(istty(x)); } -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}