I believe that a static function in a source file cannot be called directly from outside the file. However, if I somehow manage to get a pointer to this function into another file, can I then call this function from that file. If yes, is there any scenario when we would like to take this route as compared to simply making the function non-static ?
7 Answers
Yes, you can export static functions by handing out pointers to them. This is a common means of implementing the Factory pattern in C, where you can hide the implementations of a whole bunch of functions from the modules that use them, and have a FuncPtr_t GetFunction( enum whichFunctionIWant) that hands them out to consumers. That's how many dynamic linking implementations work.
Comments
However, if I somehow manage to get a pointer to this function into another file, can I then call this function from that file.
Yes, it's perfectly possible. The function isn't visible to the linker, but it's there in the executable. You can always jump to its address.
I am not sure about scenarios though. Maybe you want others to only call your function after they called some other function (non-static of course). So you have them obtain it, and then they can call it.
Comments
As the other mentioned, you can do it. An example of why you might want to is for some sort of "driver". You might pass back a structure containing the open, close, read, and write functions without having to make the actual functions publicly accessible.
1 Comment
Yes, you can. If you have to call a function that expects a pointer to a function as a callback, you can use static so that the function symbol does not pollute the global namespace and does not cause linker conflicts. The most used example is probably qsort:
struct Data { ... };
static int compareData(const void* a, const void* b) { /* cast to Data and compare */ }
...
qsort(array, count, sizeof(Data), &compareData);
1 Comment
Yes you can, you could always try it for yourself and find out:
file1.c
#include <stdio.h>
void call_hello(void (*fptr)(void));
static void hello(void) {
puts("hello");
}
int main(void)
{
void (*fptr)(void) = hello;
call_hello(fptr);
return 0;
}
file2.c
void call_hello(void (*fptr)(void))
{
fptr();
}
1 Comment
It is often useful to define a small static function as a worker function for some other one. Look at the standard qsort for an example: it expects a comparing function, and very often it is better to make that comparing function static (e.g. because it is not useful elsewhere, and because qsort wants it to have some unpretty signature).
inlinekeyword and/or proprietary compiler directives which would always inline a function.inlinebut the address is taken in a function pointer, the compiler would generate the body in the binary, while also inlining it into other functions in the same module.