lilzz
lilzz

Reputation: 5413

Function pointer with (void*)

In C what's the function pointer (void*) doing in:

    int (*fn) (void*)

If the parameter is nothing then it should be:

    int (*fn) ()

My understanding is void* is chunk of memory. void* mem means mem pointing to a chunk of memory. But what's (void*) without a name?

Upvotes: 2

Views: 1436

Answers (4)

jamesdlin
jamesdlin

Reputation: 89926

An unnamed parameter is not the same thing as no parameters. The declaration:

int (*fn)(void*);

merely states that fn is a function pointer that takes a void* argument. The parameter name is inconsequential (and is only meaningful in the function implementation where it's the name of the local variable).

(Although it the parameter name is not necessary in function declarations, it is nevertheless useful to people reading the code to identify what the parameter is.)

Upvotes: 2

user123
user123

Reputation: 9071

That function pointer declaration does not require you to give the void* a name. It only requires a type to define the argument list.

This is similar to how:

void my_function(int x);

is just as valid as

void my_function(int);

Upvotes: 6

Sanjay Manohar
Sanjay Manohar

Reputation: 7026

It means that fn is "any function that takes a block of memory as a parameter, and returns an int"

Upvotes: 1

lurker
lurker

Reputation: 58224

void * is an anonymous pointer. It specifies a pointer parameter without indicating the specific data type being pointed to.

Upvotes: 4

Related Questions