James
James

Reputation: 819

c function declaration explanation on argument with type "void(*fcn)(void*)"

I encountered the following line as a C function declaration. I'm confused by the type of the first argument passed in. Can anyone explain how to understand the type of the first argument?

int clone(void (*fcn) (void *) , void *arg, void *stack)

Upvotes: 1

Views: 1033

Answers (3)

P0W
P0W

Reputation: 47814

Use "Spiral Rule" :

      +------+
      | +--+ |
      | ^  | |
void (*fcn ) (void *)
  ^   ^    | |
  |   +----+ |
  +----------+

So,

  • fcn is a pointer to
  • a function having void * as an argument
  • returning void (nothing)

Upvotes: 3

Mihai Maruseac
Mihai Maruseac

Reputation: 21460

After a few editing (to remove the names of variables) cdecl gave the answer:

declare clone as function (pointer to function (pointer to void) returning void, pointer to void, pointer to void) returning int

Or, you can test only the first argument and get it's type: pointer to function of pointer to void, returning void.

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122443

void (*fcn) (void *)

Read the type from inside out: fcn is a pointer to a function, that function takes a void * parameter and returns nothing (void).

Upvotes: 4

Related Questions