[net.lang.c] address of function

shankar@amd.UUCP (Hq Apps) (03/15/86)

I am a programmer who has just learnt C and has started using it
to do my programming. I have a question for which I was unable to
get the answer. I would like to know how to find the address of a
function in C. I would appreciate any help in this regard from netlanders.
Thanking all the respondents(C experts).

                                                    Sincerely
                                                    N.Shankar
                                                    Applications Engineer
                                                    Advanced Microdevices
                                                    Sunnyvale, CA 94086
                                                    USA
                                                    (408)-982-6869

lvs@ndm20 (03/17/86)

>I am a programmer who has just learnt C and has started using it
>to do my programming. I have a question for which I was unable to
>get the answer. I would like to know how to find the address of a
>function in C. I would appreciate any help in this regard from netlanders.
>Thanking all the respondents(C experts).

As defined in K&R, the address of a function may be obtained by referencing
the name of the function without the parens '()', see below.

    int func();

    addr_of_func = func;

This is similar to the fact that the name of an array is it's base address.
Hope this helps.

Larry V. Streepy Jr.                        "Waiting is"
Nathan D. Maier Consulting Engineers

VOICE:  (214)739-4741
Usenet: ...!{allegra|ihnp4}!convex!smu!ndm20!lvs
CSNET:  ndm20!lvs@smu
ARPA:   ndm20!lvs%smu@csnet-relay.ARPA

jsdy@hadron.UUCP (Joseph S. D. Yao) (03/18/86)

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}