Click to See Complete Forum and Search --> : On assignment to a function pointer


Sayan Mukherjee
December 26th, 2002, 03:55 AM
Hi,

I have a the following declarations in my C code:


void func1(void); // Prototype of function func1()

long *p = ... // pointer to long which contains a valid address.
// The address is that of the function func1()

void (*fptr)(void); // This is the function pointer


This address in 'p' is, as stated above, the address of the function func1().

How do I assign from pointer 'p' to pointer 'fptr' so that the
function executes if called as given below:


fptr();


Thanks in advance.

j0nas
December 26th, 2002, 04:13 AM
Just do fptr = p

That will of course give a warning or two, unless you cast it: fptr = (void (*)(void))p.

I don't know why you need to store the func address in 'p' when you can assign fptr directly: fptr = func1. That's better coding style.

Good luck.

Sayan Mukherjee
December 26th, 2002, 05:46 AM
Hi j0nas,

Thanks for the reply. It really helped.

The code in the posting is a curtailed version of the original
code where I had the doubt.

In the actual code, I am getting a pointer as long * (from
another part of the application) containing the function's
address. I need to execute that function from this address.

Thanks again for your help.