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!!
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!!
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
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
Here is how to read these declarations
http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html
http://www.codeproject.com/cpp/complex_declarations.asp?df=100&forumid=47185&exp=0&select=827924
http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html
http://www.codeproject.com/cpp/complex_declarations.asp?df=100&forumid=47185&exp=0&select=827924
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
cdecl for Windows is downloadable here :
http://www.simtel.net/product.php?url_fb_product_page=41564
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.
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.
> float(*funk)(float[])
funk is a pointer to a function which takes a float array as argument an returns a float
Cheers!
Sunnycoder