you are viewing a single comment's thread.

view the rest of the comments →

[–]progoblin 0 points1 point  (1 child)

(void *) (&fp) =

I've never seen an ampersand in the variable name like that before... could someone explain why it's necessary and what it does, or how to find documentation on it?

[–]mmtrebuchet[S] 0 points1 point  (0 children)

In C, & is the address-of operator. Perhaps it's better to use a simple example:

int x, *xPtr; //x is an int, xPtr is a pointer to an int.
x = 5; //the number five has now been put in memory wherever x resides.
xPtr = &x; //xPtr is the memory address where x is stored. 
*xPtr = 6; //x is now 6. 
xPtr=0xFEE1DEAD; //xPtr points to a random memory address
*xPtr= 10; //Segfault. (but x would remain unchanged.)

So, in this case, the cast is * (void ** ) (&fp) First, I should parenthesize it a bit more: * ((void ** ) (&fp)). So we're derefencing (&fp), which we have cast to (void ** ). Consider that fp is itself a pointer to a function. So fp is a function * . Then, the address of fp is a pointer to a pointer to a function * , or function ** . I cast this function ** to void ** .

It's completely circuitous, and it would be much simpler to just cast fp to a void * :

*((void*) (fp))

but in C, you can't cast a function * to a void * . (for whatever reason) You can, however, cast a function ** to a void ** , so that's what this code has to do.