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.
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.
void data type. A data type that has no values or operators and is used to represent nothing.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With