tags:

views:

115

answers:

3

Hello,

I came across this in embedded hardware using C.

#define EnterPWDN(clkcon) (  (void (*)(int))0xc0080e0 ) (clkcon) 

I have no idea how is this function macro working. I understand clkcon is the function parameter to EnterPWDN, but what is happening after that?

+6  A: 

It casts the address 0xc0080e0 to a pointer to function taking an int and returning void, and calls that function, passing clkcon as the parameter.

Spelled out:

typedef void (func_ptr*)(int);
func_ptr func = (func_ptr)0xc0080e0;
func(clkcon);

(If you haven't come across function pointers, you might want to grab a good C introduction and read up on the subject.)

sbi
+5  A: 

Its a void function pointer that takes an int as a parameter. The function is held at the specific memory address 0xc0080e0.

(void (*)(int))

The above is a function pointer declaration. First comes the void return type. Next comes the fact that its a pointer and finally the int tells you what the parameter to the function is. The memory address is the location the function is stored at and the whole thing is casting that memory address into the correct function pointer type and then calling the function and passing "clkcon" to it.

Goz
+3  A: 

Excellent answers Goz and sbi, but to put it in another way:

At a specific address (0xc0080e0) in memory, possibly in a ROM, there is a function. You call this function with the clkcon argument.

Amigable Clark Kant