[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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!!
0
sandeep_th
Asked:
sandeep_th
1 Solution
 
sunnycoderCommented:
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
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sunnycoderCommented:
Hi sandeep_th,

> But I am not quite sure how to define a variable like this. Nothing
> wrong with the code incidentally, 'cause it compiled ok.

float(*funkptr)(float[]) = NULL; -- this defines a function pointer funkptr which is of the same type as in your example and initializes it to NULL. This pointer can be assigned address of any function which take a float array as prameter and returns a float ... e.g.

float func1 (float[])
{
}

funkptr = &func1; -- use of & operator is optional but recommended

This tutorial would be of immense help in learning about function pointers.
http://www.newty.de/fpt/index.html

Cheers!
Sunnycoder
0
 
fridomCommented:
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
0
 
WebstormCommented:
cdecl for Windows is downloadable here :
http://www.simtel.net/product.php?url_fb_product_page=41564
0
 
kaliyugkaarjunCommented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now