Function pointers and address of a function Function pointers and address of a function c c

Function pointers and address of a function


Given int a[10], both a and &a yield the same address, yes, but their types are different.

a is of type int[10]. When it is implicitly converted to a pointer type, the pointer is of type int* and points to the initial element of the array. &a is of type int (*)[10] (that is, a pointer to an array of ten integers). Because there can be no padding in an array, they both yield pointers with the same value, but the pointers have different types.

Functions are similar to arrays, but not entirely the same. Your function foo is of type double(double). Whenever foo is used in an expression and is not the operand of the unary & operator, it is implicitly converted to a pointer to itself, which is of type double(*)(double).

So, for all practical purposes, the name of a function and a pointer to the same function are interchangeable. There are some subtleties, all of which I discuss in an answer to "Why do all these crazy function pointer definitions all work? What is really going on?" (That question was asked about C++, but the rules for nonmember functions in C++ are the same as for functions in C.)


No, there's no extra storage dedicated to pointing to the function/array.

With most variables variable_name has a meaning other than getting the address of that variable, so you need to use &variable to get the address.

With a function or array, function_name (by itself, not followed by parentheses) doesn't have any other meaning, so there was no problem with interpreting it as taking the address of the function.

Likewise in reverse: a normal pointer needs to be dereferenced explicitly, but a pointer to a function doesn't (again, because there's no other reasonable interpretation), so given a pointer to a function like:

int (*func)(param_list);

The following are equivalent to each other -- both call whatever function func points at:

(*func)(params);func(params);


fun and &fun are exactly the same (except that sizeof(f) is illegal).a and &a are the same up to pointer arithmetic: a + 10 == &a + 1, because 10*sizeof(*a) == sizeof(a) (where sizeof(*a) == sizeof(int)).