2

I have test the following code:

#include <stdio.h>
void f(void g()) {
    g();
}

void g(void) {
    printf("hello, world\n");
}

int main() {
    f(g);
    return 0;
}

It works well. In the code above, why in c we can take the function name g as paramter and take the function prototype as parameter?

In book K&R, it says that we can use the pointer to function: So I can define the function f like this:

void f(void (*g)()) {
    g();
}
void g(void) {
    printf("hello, world\n");
}

int main() {
    void (*pf)() = g;
    f(pf);
    f(g);   // Here f(g) also works.
    return 0;
}

So in the first f definition form, the parameter prototype is void g(), in the second form the parameter prototype is void (*g)(). Why the two forms of definition of f are the same?

And in the second form, the parameter is a pointer to function, why f(g) also works?

44
  • Function names are converted to function pointers in most situations.
    – Ian Abbott
    Mar 30, 2017 at 9:11
  • In other words, we can use function names as pointers in most situations, so can I say there is no need to define pointer to functions? Then I can use function names as other languages directly.
    – zhenguoli
    Mar 30, 2017 at 9:13
  • @zhenguoli: It is like void f(int a[]), where a will actually be of type int *, not int[]. Mar 30, 2017 at 9:15
  • No you can't say that. Mar 30, 2017 at 9:20
  • @n.m. Why? Any reason or the advantage of pointer to function?
    – zhenguoli
    Mar 30, 2017 at 9:23

4 Answers 4

4

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator65) or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.

and (6.7.6.3 Function declarators (including prototypes))

8 A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

Thus these function declarations

void f(void g());

and

void f(void ( *g )());

declare the same one function.

2

Because a function decays into a function pointer when it is not used within a function calling context:

void f(void a()) { a(); }    //void a() decays into void (*a)()
void g(void (*a)()) { a(); }    //equivalent to the above
void h(void a()) { (*a)(); }    //*a is equivalent to a
void i(void a()) { (**a)(); }    //**a is equivalent to *a
void j(void a()) { (***a)(); }    //...
void k() { };

int main() {
    f(k);
    g(k);
    h(k);
    i(k);
    j(k);
}

This compiles and runs fine, even though I've been using liberal amounts of the dereferencing operator on the argument function. Each time I use it, the resulting function decays back into a pointer when it hits the next dereferencing operator. Likewise, the function argument void a() decays into a pointer just like int array[] decays into int* array when used as a function argument.

1

When used as a function parameter, function name behaves as a pointer to the function itself.
These all are valid prototype

void f(void (g)());
void f(void (*g)());
void f(void (**g)());
void f(void (*****g)());

When you declare a function pointer then you can make a call using that pointer as

g();
(*g)();
(*****g)();
2
  • So as far as I'm concerned, maybe there is no need to define pointer to functions. And there is something material explaining this standard? I am reading K&R, there hasn't talked about it.
    – zhenguoli
    Mar 30, 2017 at 9:22
  • 1
    @zhenguoli If you want a variable that can be changed, you have to use a function pointer.
    – Lundin
    Mar 30, 2017 at 9:23
1

The C standard ISO/IEC 9899:2011, section 6.3.2.1 item 4 says:

A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator,65) or the unary & operator, a function designator with type “function returning type” is converted to an expression that has type “pointer to function returning type”.

So normally a function name (or other expression with function type, i.e. a function designator) is converted to a pointer to a function, except in the circumstances mentioned above.

So what happens when you apply the & operator to a function, as that is one of the cases excluded from the above clause. The answer is given by section 6.5.3.3 item 3, the relevant parts of which are:

The unary & operator yields the address of its operand. If the operand has type “type”, the result has type “pointer to type”. [...] Otherwise, the result is a pointer to the object or function designated by its operand.

(The "[...]" part I missed out discusses applying the & operator to the result of a unary * operator and to the result of the [] operator.)

So applying & to a function name (or other function designator) also gives you a pointer to the function.

So what happens when you dereference a function pointer with the unary * operator? It produces a function designator (section 6.5.3.3 item 4), which in most circumstances (as discussed above) gets converted back to a pointer to a function!

EDIT: I neglected to mention the case of declaring a parameter of a function to be a function returning type. As discussed in Vlad from Moscow's answer referring to section 6.7.6.3 item 8, that gets automatically adjusted to be a pointer to a function returning type.

Not the answer you're looking for? Browse other questions tagged or ask your own question.