Leo Messi
Leo Messi

Reputation: 822

Dereference of function pointers

When I was browsing the Linux code I encountered the following snippet :

static void __init do_initcalls(void)
{
initcall_t *fn;

for (fn = __early_initcall_end; fn < __initcall_end; fn++)
    do_one_initcall(*fn);
}

initcall_t is a function pointer .

The prototype of do_initcalls is int do_one_initcall(initcall_t fn) .

So I thought invoking do_initcalls would be like do_one_initcall(fn) but I see it is do_one_initcall(*fn) . Why is that *fn instead of only fn??

Upvotes: 1

Views: 427

Answers (1)

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

Because initcall_t is itself defined as a function pointer, initcall_t *fn declares a pointer to a function pointer, and thus the * dereferencing operator is applied to get the function pointer.

Here is the definition of the initcall_t type:

typedef int (*initcall_t)(void);

So the type initcall_t is already a pointer.

Upvotes: 4

Related Questions