In C, what is the difference between `&function` and `function` when passed as arguments?

后端 未结 4 1693
醉酒成梦
醉酒成梦 2020-12-05 07:41

For example:

#include 

typedef void (* proto_1)();
typedef void proto_2();

void my_function(int j){
    printf(\"hello from function. I got          


        
4条回答
  •  执笔经年
    2020-12-05 08:29

    The difference is only stylistic. You have the same scenario when using function pointers:

    void func (void);
    

    ...

    void(*func_ptr)(void) = func;
    
    func_ptr();    // call func
    (*func_ptr)(); // call func
    
    printf("%d\n", ptr); 
    printf("%d\n", *ptr);
    

    There are some who say that the (*func_ptr)() syntax is to prefer, to make it clear that the function call is done through a function pointer. Others believe that the style with the * is clearer.

    As usual, there are likely no scientific studies proving that either form is better than the other, so just pick one style and stick to it.

提交回复
热议问题