3

Just recently I have been asked in an interview: Where can we use function pointers in C? And what function pointer returns. I said using we can call functions using function pointer then he asked some example but I could not satisfy with him some more example. He then asked me what function pointer return. I told him it depends upon the declaration of function pointers.

But I really would like to know some uses of function pointer in C.

2

4 Answers 4

14

I think the classical example in C is qsort. Quoting from there (and I DO know it's http://www.cplusplus.com, so a not-very-good site, but it seems to be correct)

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

Function that compares two elements. The function shall follow this prototype:

int comparator ( const void * elem1, const void * elem2 ); 

The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.

The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.


The other "classical" example is the calculator (for example see this, it's C++ but it's the same in C).

You have four math functions, for example

float Plus    (float a, float b) { return a+b; }
float Minus   (float a, float b) { return a-b; }
float Multiply(float a, float b) { return a*b; }
float Divide  (float a, float b) { return a/b; }

in some way you select your operation

/* Here there should be an if or a switch/case that selects the right operation */
float (*ptrFunc)(float, float) = Plus;

and you can call it later (for some reason you don't want to call it directly in the if/switch, perhaps because you want to make other "things" that are "common" to all the operations, like logging or printing the result)

float result = ptrFunc(1.0f, 2.0f);

Another two things you can use function pointers for are callbacks (as written by vine'th) and "poor man" virtual functions (you put them in a struct and when you create the struct (you know, like using a "poor man" constructor, but this is C so we will call it an initializer), you save there what functions will be used by that struct).

0
4

If you don't have inheritance/classes etc you could emulate that with C. For example call different functions depending on some condition.

You can also put function pointers as arguments to some other function. For example if you have a sorting algorithm, you can provide a compare function via function pointer.

And yes, you're correct about the return value. This depends on the definition.

2

Function pointers are used in asynchronous notifications, for example, a general paradigm is to pass a function pointer and a callback data which will be called back by the library. You can see an example in the glib mainloop; In C++ STL, you can have a look at the widely used functors and a function pointer is a functor.

0

When I think of using function pointers, I think of using them in terms of call outs from code that is generic in nature into code that is specialized or custom in nature.

Consider qsort(). This is a generic sorting algorithm. Using a function pointer to compare elements allows the user to customize qsort() to their particular data type.

Consider file systems. Most systems use a virtual file system framework to handle them. The framework provides the common functionality. To handle the differences, the framework makes callouts via function pointers to file system specific code.

Not the answer you're looking for? Browse other questions tagged or ask your own question.