[comp.lang.c] Static Function Invocation Thru Pointer

labc-1ia@web-3g.berkeley.edu (Andrew M. Choi) (03/03/91)

Hi.  This article is about the question of invoking a static
function in another file through the use of pointer to
function.  Please consider the following:

/* In file "foo.c" */

int (*function_pointer)();

static int function()
{
	printf("This is from function\n");
}

setFunctionPointer()
{
	function_pointer = function;
}

/* In file "bar.c" */

main()
{
	extern int (*function_pointer)();

	setFunctionPointer();
	(*function_pointer)();	/* Is this legal?  Note that "function" */
				/* is a static function .. unknown to */
				/* "bar.c" */
}

Personally, I think the above is *ALWAYS* legal (can't think of any
reason why this will fail).  Is this true for all machine
architectures?

I thank you in advance for your help.


Name:  Andrew Choi	Internet Addr:  achoi@cory.berkeley.edu
Tel:   (415)848-5658
#include <std/disclaimer.h>

henry@zoo.toronto.edu (Henry Spencer) (03/03/91)

In article <1991Mar3.002250.26164@agate.berkeley.edu> labc-1ia@web-3g.berkeley.edu (Andrew M. Choi) writes:
>Hi.  This article is about the question of invoking a static
>function in another file through the use of pointer to
>function...

That is perfectly legitimate.  If you can get a pointer to it, you can
call it.  "static" just means that the *name* is not visible outside the
file where it is defined.
-- 
"But this *is* the simplified version   | Henry Spencer @ U of Toronto Zoology
for the general public."     -S. Harris |  henry@zoo.toronto.edu  utzoo!henry

pausv@sssab.se (Paul Svensson) (03/04/91)

henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <1991Mar3.002250.26164@agate.berkeley.edu> labc-1ia@web-3g.berkeley.edu (Andrew M. Choi) writes:
>>Hi.  This article is about the question of invoking a static
>>function in another file through the use of pointer to
>>function...
>
>That is perfectly legitimate.  If you can get a pointer to it, you can
>call it.  "static" just means that the *name* is not visible outside the
>file where it is defined.

I had some fun(?) with this when (de)porting Hack to bsd 2.9...

Henry is right, of course, but you shouldn't trust all C implementators to
know this.  CC on the pdp/11 did this wrong in one case; whe using overlays,
functions declared static did not get a "thunk" in the base overlay, and when
a pointer to a static function was passed off to someone in another overlay,
using it there had interesting results :-)

			/Paul
-- 
Paul Svensson   _   /|   - Every absurdity needs a champion to defend it -
SM5SJS          \'o.0'   Scandinavian System Support    Fax: +46 13 115193
paul@sssab.se   =(___)=  Box 535        _             Phone: +46 13 111660
sunic!sssab!paul   U     SE-581 06  Linkoping, Sweden  Home: +46 13 121021