A line of code has baffled me, and I cannot resolve it. It could be casting of a function address and assigning it to a function pointer, but then 'address' should not be there. Or am I completely out of context?
int32_t (*const my_func)(uint32_t address) = (int32_t (*)(uint32_t address)) nvm_addr;
Function Pointer Syntaxvoid (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.
In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type.
We declare the function pointer, i.e., void (*ptr)(char*). The statement ptr=printname means that we are assigning the address of printname() function to ptr. Now, we can call the printname() function by using the statement ptr(s).
int foo(int); Here foo is a function that returns int and takes one argument of int type. So as a logical guy will think, by putting a * operator between int and foo(int) should create a pointer to a function i.e. int * foo(int);
int32_t (*const my_func)(uint32_t address)
That a variable called my_func
which stores a const pointer to a function taking a uint32_t
and returning an int32_t
. The parameter name is optional, it's just there to give an idea of the semantics of that parameter.
(int32_t (*)(uint32_t address)) nvm_addr
That casts nvm_addr
to a pointer to a function of the same type as my_func
.
Overall, this is just a rather verbose way of storing a function pointer to nvm_addr
.
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