what is this definition?

  • Thread starter Guybrush Threepwood
  • Start date
G

Guybrush Threepwood

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]);

//....

}
 
J

Jakob Bieling

Guybrush Threepwood said:
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 *) )

'basic_func' is a pointer to a function returing a double and taking two
'data_point' pointers as its arguments. To make it clearer, you usually use
a typedef and use the typdef'd name in the function:

typedef double (*pbasic_func)(data_point *, data_point *);
void makematrix_poly( pbasic_func basic_func )
{
//....

matrix[pi+1][si+1] = (*basic_func)(&points[pi], &points[si]);

//....

}

hth
 
D

David White

Guybrush Threepwood said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,435
Latest member
PhilipBelm

Latest Threads

Top