1

Using typedef for functions, like in the following example, are there any differences between using the address of the function or only the function?

#include <stdio.h>

int foo(int i){return i+1;}

typedef int(*mytype[2])(int);

int main(void)
{
    mytype f;

    f[0] = foo;
    f[1] = &foo;
    printf("%d %d", f[0](5), f[0](6));

    return 0;
}
untitled
  • 389
  • 4
  • 13
  • See e.g. [this function to pointer conversion reference](http://en.cppreference.com/w/c/language/conversion#Function_to_pointer_conversion). – Some programmer dude Feb 10 '16 at 11:54
  • @JoachimPileborg The reference seems incorrect. Surely, a function designator which is operand to the function call operator (`()`) also does not undergo conversion. – fuz Feb 10 '16 at 11:56
  • 1
    @FUZxxl: Are you sure? The postfix operator `()` expects a "pointer to function" as its operand in my version of the C standard. – undur_gongor Feb 10 '16 at 11:59
  • @undur_gongor Interesting. You never stop learning. – fuz Feb 10 '16 at 11:59

1 Answers1

3

In the C language, functions are implicitly converted to function pointers in most contexts. Thus you see no difference between f[0] = foo and f[1] = &foo. I prefer the latter convention, but really, both are equally fine.

fuz
  • 88,405
  • 25
  • 200
  • 352