Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does void(*) in C++ mean anything?

I'm trying understand C++ function pointer syntax. In Eclipse on Linux when I typed:

void(*);

It highlighted the statement with a message saying syntax error, but it let me compile it and the program ran. Then on Visual Studio I tried it and it won't compile, saying "Expected an expression". However what's strange is that when I do:

std::vector<void(*)> myVector;

It compiles fine on Visual Studio. Also on a couple of online compilers void(*); on its own works fine. I know that:

void (*)();

... is a function pointer and..

void();

... is a function signature, which is why you can do:

std::function<void()> func;

I'm having a lot of trouble understanding function pointer syntax.

Thanks.

like image 273
Zebrafish Avatar asked Dec 26 '16 03:12

Zebrafish


People also ask

What type is a void * in C?

The void type, in several programming languages derived from C and Algol68, is the return type of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects, such as performing some task or writing to their output parameters.

What is void * data type?

void data type. A data type that has no values or operators and is used to represent nothing.

What is a void * pointer?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is a void * return type?

A void return type simply means nothing is returned. System. out. println does not return anything as it simply prints out the string passed to it as a parameter.


1 Answers

Remember that parentheses can be used to change the precedence of certain things. That's why you have the parentheses around the asterisk in void (*)() because it's very different from void *().

In the case of void(*) the parentheses are such precedence-changing parentheses. But they are not needed. The type void(*) is void*, plain and simple.

The context where you use it is important though.

like image 77
Some programmer dude Avatar answered Sep 20 '22 09:09

Some programmer dude