Aldee
Aldee

Reputation: 4518

What is the function of an asterisk before a function name?

I've been confused with what I see on most C programs that has unfamiliar function declaration for me.

void *func_name(void *param){
    ...
}

What does * imply for the function? My understanding about (*) in a variable type is that it creates a pointer to another variable thus it can be able to track what address at which the latter variable is stored in the memory. But in this case of a function, I don't know what this * asterisk implies.

Upvotes: 85

Views: 59020

Answers (4)

Vincenzo Pii
Vincenzo Pii

Reputation: 19805

The * refers to the return type of the function, which is void *.

When you declare a pointer variable, it is the same thing to put the * close to the variable name or the variable type:

int *a;
int* a;

I personally consider the first choice more clear because if you want to define multiple pointers using the , separator, you will have to repeat the * each time:

int *a, *b;

Using the "close to type syntax" can be misleading in this case, because if you write:

int* a, b;

You are declaring a pointer to int (a) and an int (b).

So, you'll find that syntax in function return types too!

Upvotes: 28

x4u
x4u

Reputation: 14077

The * belongs to the return type. This function returns void *, a pointer to some memory location of unspecified type.

A pointer is a variable type by itself that has the address of some memory location as its value. The different pointer types in C represent the different types that you expect to reside at the memory location the pointer variable refers to. So a int * is expected to refer to a location that can be interpreted as a int. But a void * is a pointer type that refers to a memory location of unspecified type. You will have to cast such a void pointer to be able to access the data at the memory location it refers to.

Upvotes: 16

hmjd
hmjd

Reputation: 121961

It means that the function returns a void*.

Upvotes: 5

NPE
NPE

Reputation: 500257

The asterisk belongs to the return type, and not to the function name, i.e.:

void* func_name(void *param) { . . . . . }

It means that the function returns a void pointer.

Upvotes: 96

Related Questions