Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!uwvax!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: What can I get away with Message-ID: <7270@mimsy.UUCP> Date: Tue, 30-Jun-87 21:28:20 EDT Article-I.D.: mimsy.7270 Posted: Tue Jun 30 21:28:20 1987 Date-Received: Thu, 2-Jul-87 00:39:46 EDT References: <608@zen.UUCP> <2299@hoptoad.uucp> <21211@sun.uucp> <830@omepd> <8190@linus.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 70 In article <8190@linus.UUCP> jfjr@mitre-bedford.ARPA (Jerome Freedman) writes: >Suppose I declare an array of pointers to functions in one module. >I write a function, in the same module, which will install functions >in this array -call the function "install". >In a different module I write a function, call it "new_function". >I make NO attempt at making "new_function" visible anywhere outside >of the module in which it is written. You have to do something special to make it invisible outside its own module, namely, declare it `static'. >I call install on this new_function to put it in my array. ... >Can I then go through this array of function pointers ... and call >the functions pointed to even if they are not declared externally >... ? Certainly. % cat f1.c static void (*a[10])(); static int n_funcs; int install(f) void (*f)(); { if (n_funcs >= sizeof (a) / sizeof (a[0])) return (-1); /* out of space */ a[n_funcs] = f; return (n_funcs++); } void call_all() { register int i; for (i = 0; i < n_funcs; i++) (*a[i])(); } % cat f2.c /*ARGSUSED*/ static void new_function() { printf("new_function\n"); } int main(argc, argv) int argc; char **argv; { (void) install(new_function); call_all(); return (0); } % cc f1.c f2.c % a.out new_function % This assumes your compiler can handle `void (*p[N])()'. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris