Functions as function parameter

R

Rolf Wester

Hi,

I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.

Regards

Rolf

int comp_func1(double * a1, double * a2)
{
...
}

void my_qsort(double * a, int n, (* comp_func))
{ ???????

...
comp_func(&a, &a[j]);
...
}


int main()
{
...
my_qsort(a,n,comp_func1);
}
 
J

Joona I Palaste

Rolf Wester said:
I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.


int comp_func1(double * a1, double * a2)
{
...
}
void my_qsort(double * a, int n, (* comp_func))
{ ???????

void my_sqsort(double *a, int n, int (*comp_func)(double *a1, double *a2))
 
B

Barry Schwarz

Hi,

I want to pass a C-function as a function parameter but I don't know

You cannot pass a function as an argument in a function call. You
can, however, pass a pointer to function which will probably let you
do everything you had in mind originally.

If that is your intent, look up qsort and see how it declares the
function it receives as an argument. Most C manuals usually describe
how to set such a function up also.
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?

snip


<<Remove the del for email>>
 
R

ranjeet

Rolf Wester said:
Hi,

I want to pass a C-function as a function parameter but I don't know
how to that correctly. In the example below how would I have to declare
the function argument in the my_sort function definition?
Thank you in advance for any help.

First Of always remember that you can not pass the function as the
parameter to another function as the argument. Rather we Pass Only the
address (Pointer) of the Fucntion as an argument.
Regards

Rolf

int comp_func1(double * a1, double * a2)
{
...
}

void my_qsort(double * a, int n, (* comp_func))
Void my_qsort(double *a, int n, (*comp_func)(double * a1, double * a2))
{ ???????
>
...
comp_func(&a, &a[j]);
...
}


int main()
{
...
my_qsort(a,n,comp_func1);
}
 
D

Dave Thompson

Rolf Wester <[email protected]> scribbled the following:
void my_sqsort(double *a, int n, int (*comp_func)(double *a1, double *a2))

Or you can omit the parameter names, if you prefer:
void my_sqsort (double *a, int n,
int (*comp_func)(double *, double *) )
Since the parameter names aren't used/usable within this function
(only the function to which this parameter points) this is arguably
clearer; however it differs from the target function definition (even)
if that is (also) in prototype format which you may dislike.

Or you can omit the parameter description entirely:
void my_sqsort (double *a, int n, int (*comp_func) () )
which is less safe as it doesn't (at least isn't required to) check
compatibility of calling signatures, but more powerful as it allows
you to pass functions (pointers) with differing signatures -- and you
must take responsibility for ensuring that you call them correctly.
- David.Thompson1 at worldnet.att.net
 

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,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top