"Guybrush Threepwood" <sa***@sco.com> wrote in message
news:c2**********@newsg4.svr.pol.co.uk...
double ** matrix;
struct data_point
{
float x,y,z;
float nx,ny,nz;
float value;
};
Hi I have the following function definition. My question is what is
basic_func in that definition?
I have never seen this before. Can anyone tell me what it means?
matrix and data_point you can see them declared above, and inside the
function this is the only other place where basic_func is mentioned in
the code I am pasting from.
void makematrix_poly( double (*basic_func)(data_point *, data_point *) )
{
//....
matrix[pi+1][si+1] = (*basic_func)(&points[pi], &points[si]);
//....
}
double (*basic_func)(data_point *, data_point *)
Starting from the identifier (basic_func), the first operator in order of
precedence is * (dereference), so basic_func is a pointer (i.e., in an
expression, * dereferences the pointer to yield the object it points to).
The next operator is () (function call), so basic_func is a pointer to a
function. The rest of it is like any other function (parameters and return
type). So the complete type is: pointer to a function taking parameters
data_point *, data_point * and returning a double.
Note that the parentheses around *basic_func are necessary because the
function call parentheses have higher precedence than *. If they weren't
there you would have:
double *basic_func(data_point *, data_point *)
This time, the function call parentheses have the highest precedence, so
basic_func is a function. The * comes next, so it's a function returning a
pointer. The whole type is: function taking parameters data_point *,
data_point * and returning a double*.
(*basic_func)(&points[pi], &points[si]);
In the actual call, the pointer to function is dereferenced (*basic_func),
yielding the function itself. The rest is the same as an ordinary function
call.
DW