1

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?

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • In C a function is a function is a function. There's nothing to make some functions (except `main`) more "special" than any other function, or to annotate functions to be used only for special purposes. All you have is comments and documentation. – Some programmer dude Mar 17 '17 at 09:56
  • 1
    For example, you can use like Adam Dunkels did in protothreads `PT_THREAD(example(struct pt *pt))` for declaring function with name `example`. More here: http://dunkels.com/adam/pt/ – unalignedmemoryaccess Mar 17 '17 at 09:56
  • That sounds like it could be an answer, not just acomment (+1) – Mawg says reinstate Monica Mar 17 '17 at 09:57
  • 1
    `extern specialFunction mySpecialFunction(void);` is legal code. It declares `mySpecialFunction` as a function which takes no parameters, and returns a function pointer. It's not clear from your question whether this is what you intended – M.M Mar 17 '17 at 11:18
  • Oops, sorry, no. That is not what I intended (+1). I intended to show that I can't declare a fucntoin with the signature of the typedef, using the typedef. – Mawg says reinstate Monica Mar 17 '17 at 11:23

3 Answers3

2

You can't do anything in this. However, for visual representation, you can use self definitions like Adam Dunkels in protothreads:

typedef void (* specialFunction) (void);
#define TIMERCALLBACK(x)         specialFunction x

and then declare function like this:

TIMERCALLBACK(mycallback(void)) {
    //do your job
}

This is just one way how to represent function as timer callbackfor example.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
  • What is stopping you from declaring `TIMERCALLBACK(mycallback(int))` which then isn't a specialFunction? – Chris Turner Mar 17 '17 at 10:12
  • I did not quite understand your question here. – unalignedmemoryaccess Mar 17 '17 at 10:53
  • You're not actually defining `mycallback` to be `void mycallback(void)` and thus be the same type as specialFunction - you're declaring it to return a `specialFunction` which isn't what the OP wants. – Chris Turner Mar 17 '17 at 10:59
  • This is true, it can be easuly used just with `void`. But in C is no way to do what he want, thus I showed him another principle how to do visual representation. – unalignedmemoryaccess Mar 17 '17 at 11:00
  • 1
    This code declares `mycallback` as a function which returns a function pointer ; however I think OP wanted to declare `mycallback` as a function returning `void` – M.M Mar 17 '17 at 11:15
1

You can't declare functions like that, only variables. But you can use a macro to ensure your functions are declared correctly like this.

typedef void (* specialFunction) (void);
#define SPECIALFUNCTION(x) void x(void)

extern SPECIALFUNCTION(mycallback);

int main(void)
    {
    specialFunction foo=mycallback;
    foo();
    }

SPECIALFUNCTION(mycallback)
    {
    printf("test\n");
    }
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
1

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.

M.M
  • 138,810
  • 21
  • 208
  • 365