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 ?
-
2I wonder what the behavior is if you mix this with the `inline` keyword and/or proprietary compiler directives which would always inline a function. – mpontillo Jan 25 '14 at 02:18
-
1@mpontillo I believe that even if the function is marked as `inline` but 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. – Ajay Brahmakshatriya May 09 '17 at 04:58
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.

- 40,496
- 12
- 101
- 170
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.

- 178,505
- 25
- 365
- 392
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.

- 19,673
- 4
- 43
- 72
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);

- 9,216
- 1
- 26
- 33
Yes, a non-static function in your file can return a pointer to a static function to anywhere you like.

- 97,814
- 39
- 186
- 310
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();
}

- 18,014
- 6
- 40
- 44
-
10That's often not a good approach in C. "It works" and "it's safe, will work everywhere, and doesn't rely on undefined behaviour" are often very different things. – Craig Ringer Aug 30 '13 at 05:43
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).

- 223,805
- 18
- 296
- 547