Static function access in other files Static function access in other files unix unix

Static function access in other files


It depends upon what you mean by "access". Of course, the function cannot be called by name in any other file since it's static in a different file, but you have have a function pointer to it.

$ cat f1.c/* static */static int number(void){    return 42;}/* "global" pointer */int (*pf)(void);void initialize(void){    pf = number;}$ cat f2.c#include <stdio.h>extern int (*pf)(void);extern void initialize(void);int main(void){    initialize();    printf("%d\n", pf());    return 0;}$ gcc -ansi -pedantic -W -Wall f1.c f2.c$ ./a.out42


It could be called from outside the scope via function pointer.

For example, if you had:

static int transform(int x){    return x * 2;}typedef int (*FUNC_PTR)(int);FUNC_PTR get_pointer(void){    return transform;}

then a function outside the scope can call get_pointer() and use the returned function pointer to call transform.


No, unless there's a bug in the compiler. Normally the static function code is not tagged with a name used for exporting the function in the object file, so it is not presented to the linker and it just can't link to it.

This of course only applies to calling the function by name. Other code within the same file can get the function address and pass it into a non-static function in another file and then the function from another file can call your static function.