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

CFunctionPointers

C Problem Overview


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.

C Solutions


Solution 1 - C

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.

Solution 2 - C

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!

Solution 3 - C

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.

Solution 4 - C

It means that the function returns a void*.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAldeeView Question on Stackoverflow
Solution 1 - CNPEView Answer on Stackoverflow
Solution 2 - CVincenzo PiiView Answer on Stackoverflow
Solution 3 - Cx4uView Answer on Stackoverflow
Solution 4 - ChmjdView Answer on Stackoverflow