Search code examples
cfunctionpointersmaintainability

How to show that function belongs to a typedef in C?


If I have a typedef for a function pointer, such as

typedef void (* specialFunction) (void);

how can I show that I am declaring a function of that type, and not just coincidentally a function with the same signature?

I am not trying to enforce anything, just to make the code more legible (and maintainable), and make it obvious that the function declaration is say, a timer callback, or ISR routine.

Obviously, I can't

extern specialFunction mySpecialFunction(void);

but is there any way that I can use specialFunction in the declaration, to distinguish mySpecialFunction from myBoringlyNormalFunction?


Solution

  • void (* specialFunction) (void); is a pointer type. You cannot declare a function of pointer type. I assume you mean that you want to declare a function like void f(void); but based on that typedef.

    If so, you could make the typedef be a function type:

    typedef void specialFunction(void);
    

    Then you can declare a function of that type and a pointer to such function like this:

    specialFunction func_name;
    
    specialFunction *p_func = &func_name;
    

    Many people feel that avoiding pointer typedefs makes code easier to read, because the presence of the * symbol clearly indicates whether or not we are working with a pointer.