Recource Video
Thanks for the youtube channel, The Cherno
. This post is basically a note I take to remind me what Function Pointer
is about.
Function Pointer
is a way to assign pointer to a variable.
Pass a function as a parameter.
The usual way about function
The implementation of a regular function should look like.
1 |
|
The result should be like
What if we try to use auto key for this function?
then you will see an error at the auto
.
That is because what HelloWorld()
return is a void
.
But what if we do like this. auto function = HelloWorld
then the error disapper.
This is because HelloWorld
now return a Function Pointer
instead of void
.
so if we try to print the variable function in the next line, we will get the pointer of the HelloWorld
function.
Due to the implicitly conversion,
1 | auto function = HelloWorld; |
What Function Pointer is about
Functions are of course just CPU instructions, and they are stored in somewhere in our binary/executable file.
Find the Hello World
function inside the CPU instruction and give it a pointer
, to remember the location of this function code.
so here we can assign a function to a varible.
But what type is this
auto
actually?
Here the auto hold the type write like
1 | void(*function)() |
void(*)()
is the type eventually, but since we have to get it a name so it became void(*function)()
.
It works with any other name.(Like I named it jim here)
Becauce this syntax is kinda confusing, programmer tend to either
- use
auto
, like what we did above - use
typedef
/using
it
Here we make HelloWorld function require one int
parameter
Back to programmer want to use function pointer in the first place
Programmers can pass one function to another function as a variable.
Here this code also show how to use lambda function
instead of writing out the full function body.
Reference: Cherno’s video