Function arguments

L

lancer6238

Hi,

I was looking at an open-source program and saw a function header
defined as:

int Function(int value1, int value2, (int *A)(void *A1, int *A2, void
*A3), void *A3)

and the function is called by

Function(value1, value2, A, A3);

Is the "(int *A)(void *A1, int *A2, void *A3)" in the function header
equivalent to passing a function (A) to another function (Function)?
If so, how are the arguments to A passed into Function when Function
is called?

Thank you.

Regards,
Rayne
 
I

Ian Collins

Hi,

I was looking at an open-source program and saw a function header
defined as:

int Function(int value1, int value2, (int *A)(void *A1, int *A2, void
*A3), void *A3)
Are you sure that wasn't int (*A)(void *A1, int *A2, void *A3) ?
and the function is called by

Function(value1, value2, A, A3);

Is the "(int *A)(void *A1, int *A2, void *A3)" in the function header
equivalent to passing a function (A) to another function (Function)?

Kind of, it's passing a pointer to a function.
If so, how are the arguments to A passed into Function when Function
is called?
The answer to that lies in the source of Function. The parameters may
not be passed in, they may be local to Function. For example:


int Function(int value1, int value2,
int (*A)(void *A1, int *A2, void *A3),
void *A3)
{
int a;
int b;

return A( NULL, &a, &b );
}

int A(void *A1, int *A2, void *A3)
{
return 0;
}

int main()
{
Function( 1, 2, A, NULL );
return 0;
}
 
A

Amandil

Hi,

I was looking at an open-source program and saw a function header
defined as:

int Function(int value1, int value2, (int *A)(void *A1, int *A2, void
*A3), void *A3)

and the function is called by

Function(value1, value2, A, A3);

Is the "(int *A)(void *A1, int *A2, void *A3)" in the function header
equivalent to passing a function (A) to another function (Function)?
If so, how are the arguments to A passed into Function when Function
is called?

Thank you.

No. When the argument A (which is a function taking as parameters a
void *, an int *, and another void *, and returning an int) is passed,
the compiler actually passes the address of the function of type A.
During the call to Function, A does not get any arguments. Within the
body of Function, it may call A, passing as arguments whatever it
pleases. For example:

int yada(void *v1, int *i, void *v2)
{
/* Whatever this function does */
return 42; /* Or whatever */
}

int Func(int value1, int value2, (int *A)(void *A1, int *A2, void
*A3), void *A3)
{
/* This function sets up some parameters */
int ret;

ret = A(&value1, &ret, &value2); /* treat value[1,2] as
void's */
/* Note that I don't have to write (*A)(...) */

return ret;
}

And in main(), the call looks like:

Function(value1, value2, A, A3);

Look up "function pointers" in your favorite C tutorial if you don't
understand, or feel free to come back and ask again.

-- Marty Amandil

"'Once the rocket's are up, who cares where zey come down?
Zat's not my department!' says Werner von Braun"
-- Tom Lehrer
 

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
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top