address of function

Joseph S. D. Yao jsdy at hadron.UUCP
Tue Mar 18 11:56:32 AEST 1986


In article <2287 at amd.UUCP> shankar at 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 at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.lang.c mailing list