Link to home
Start Free TrialLog in
Avatar of sandeep_th
sandeep_th

asked on

Interesting declaration

Hi,
I found the following function declaration in a piece of code I was reviewing:

void amoeba(float **p, float y[], int ndim, float ftol, float (*funk)(float []), int *nfunk)

While the rest is self-explanatory, I don't get the following parameter:

float(*funk)(float[])

I basically want to declare a variable of this kind, and pass it on to this function as parameter. But I am not quite sure how to define a variable like this. Nothing wrong with the code incidentally, 'cause it compiled ok.

Somebody please explain!!
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi sandeep_th,

> float(*funk)(float[])
funk is a pointer to a function which takes a float array as argument an returns a float

Cheers!
Sunnycoder
Hi sandeep_th,

The function is expecting a function name as a parameter.  The function returns a float and passes an array of floats.

This is a pretty common technique in advanced C.  And it has several uses.

You could be calling amoeba from several places.  Perhaps you want a different error handler depending on where it's called.  If so, you simply pass the error routine.

A very popular technique is to create an array of function addresses.  Then call the correct function by index.


You'll see this kind of thing a lot as you see more C.  At first, it's kind of daunting, and if done poorly it's INCREDIBLY tough to debug, but it sure does make some otherwise tough things very elegant.


Good Luck!
Kent
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It was explained to you by the others, but computer programmrs won't sleep untile they wrote something to explain that to you, the result is cdecl
And it tells you:
explain float(*funk)(float[])
declare funk as pointer to function (array of float) returning float

So if you are unsure next time, try cdecl and let it help you ;-)

Regards
Friedrich
Avatar of Webstorm
Webstorm

cdecl for Windows is downloadable here :
http://www.simtel.net/product.php?url_fb_product_page=41564
HI ,
funk is a pointer to a function that takes a float array as argument an returns a float
this cocenpt is preety useful where u wanna send function as a parameter to another function ...u can use da pointer  funk as a n argument to another function. :D..tha's it.