Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast to function pointer

I have come across the line of code shown below.

I think it may be a cast to a function pointer that returns void and takes a void pointer. Is that correct?

(void (*)(void *))SGENT_1_calc 
like image 824
Toby Avatar asked Apr 04 '13 09:04

Toby


People also ask

Can you cast function pointers?

Yes, it can. This is purpose of casting function pointers, just like usual pointers. We can cast a function pointer to another function pointer type but cannot call a function using casted pointer if the function pointer is not compatible with the function to be called.

What does casting a pointer mean?

A pointer is an arrow that points to an address in memory, with a label indicating the type of the value. The address indicates where to look and the type indicates what to take. Casting the pointer changes the label on the arrow but not where the arrow points.


1 Answers

Yes, it is correct. I find that not very readable, so I suggest declaring the signature of the function to be pointed:

 typedef void sigrout_t(void*); 

I also have the coding convention that types ending with rout_t are such types for functions signatures. You might name it otherwise, since _t is a suffix reserved by POSIX.

Later on I am casting, perhaps to call it like

 ((sigrout_t*) SGENT_1_calc) (someptr); 
like image 145
Basile Starynkevitch Avatar answered Sep 25 '22 04:09

Basile Starynkevitch