Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax - probable function pointer

Tags:

c++

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;
like image 234
Makan Tayebi Avatar asked Jul 17 '15 13:07

Makan Tayebi


People also ask

What is the syntax of function pointer?

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.

What is pointer give its syntax with example?

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.

How do you write a function pointer in C++?

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).

How do you declare a pointer to a function?

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);


1 Answers

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.

like image 69
TartanLlama Avatar answered Oct 20 '22 01:10

TartanLlama