40

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 ?

2
  • 3
    I wonder what the behavior is if you mix this with the inline keyword and/or proprietary compiler directives which would always inline a function. Commented Jan 25, 2014 at 2: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. Commented May 9, 2017 at 4:58

7 Answers 7

42

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.

Sign up to request clarification or add additional context in comments.

Comments

11

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

6

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

behaviour like C++ class member function. oop
5

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

This example is great!
4

Yes, a non-static function in your file can return a pointer to a static function to anywhere you like.

Comments

2

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

That'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.
2

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.