Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function pointers in C - nature and usage

I have just read an interesting question here that makes me wonder about two more things:

  1. Why should anyone compare function pointers, given that by conception, functions uniqueness is ensured by their different names?
  2. Does the compiler see function pointers as special pointers? I mean does it see them like, let's say, pointers to void * or does it hold richer information (like return type, number of arguments and arguments types?)
like image 955
Benjamin Barrois Avatar asked Jan 29 '18 06:01

Benjamin Barrois


People also ask

What is the use of function pointers in C?

Function Pointers point to code like normal pointers. In Functions Pointers, function's name can be used to get function's address. A function can also be passed as an arguments and can be returned from a function.

What are function pointers used for?

Function Pointers are pointers, i.e. variables, which point to an address of a function. The running programs get a certain space in the main memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is also an address.

What is the most useful application of function pointers?

Function pointers can be useful when you want to create callback mechanism, and need to pass address of a function to another function. They can also be useful when you want to store an array of functions, to call dynamically for example.

What is the difference between function pointer and pointer to function?

A function to pointer is one which returns a pointer. A pointer to function points to a function.


2 Answers

Why should anyone compare function pointers? Here's one example:

#include <stdbool.h>  /*  * Register a function to be executed on event. A function may only be registered once.  * Input:  *   arg - function pointer  * Returns:  *   true on successful registration, false if the function is already registered.  */ bool register_function_for_event(void (*arg)(void));  /*  * Un-register a function previously registered for execution on event.  * Input:  *   arg - function pointer  * Returns:  *   true on successful un-registration, false if the function was not registered.  */ bool unregister_function_for_event(void (*arg)(void)); 

The body of register_function_for_event only sees arg. It doesn't see any function name. It must compare function pointers to report someone is registering the same function twice.

And if you want to support something like unregister_function_for_event to complement the above, the only information you have is the function address. So you again would need to pass it in, and compare against it, to allow removal.

As for the richer information, yes. When the function type contains a prototype, it's part of the static type information. Mind you that in C a function pointer can be declared without a prototype, but that is an obsolescent feature.

like image 52
StoryTeller - Unslander Monica Avatar answered Oct 02 '22 15:10

StoryTeller - Unslander Monica


  1. Why would someone compare pointers? Consider the following scenario -

    You have an array of function pointers, say it is a call back chain and you need to call each one of them. The list is terminated with a NULL (or sentinel) function pointer. You need to compare if you have reached the end of the list by comparing with this sentinel pointer. Also, this case justifies previous OPs concern that different functions should have different pointers even if they are similar.

  2. Does the compiler see them differently? Yes. The type information includes all the information about the arguments and the return type.

    For example, the following code will/should be rejected by the compiler -

    void foo(int a); void (*bar)(long) = foo; // Without an explicit cast 
like image 25
Ajay Brahmakshatriya Avatar answered Oct 02 '22 17:10

Ajay Brahmakshatriya