-2

There is this C code:

*(*(A+i)+j) = aaa(*bbb,0,PI)/(16*PI);

aaa is a function, and bbb is another function:

double bbb(double x, double y)
{
    int i,j,k,l,m,n;
    *(K+1)=sin(x)*cos(y);
    *(K+2)=sin(x)*sin(y);
    *(K+3)=cos(x);
...
...

My question is, when bbb is called inside the function aaa, there isn't any parentheses following bbb, that is, no variable is passed into function bbb. So what are the values of x and y in the function bbb? Both zero?

Alright, this is part of a really long code:

*(*(A+i)+j) = aaa(*bbb,0,PI)/(16*PI);

aaa and related functions:

double aaa(double (*func)(double, double), double x1, double x2)
{
    double qgaus(double (*func)(double), double a, double b);
    double f1(double x);

    nrfunc=func;
    return qgaus(f1,x1,x2);
}
double f1(double x)
{
    double qgaus(double (*func)(double), double a, double b);
    double f2(double y);
    double yy1(double),yy2(double);

    xsav=x;
    return qgaus(f2,yy1(x),yy2(x));
}
double f2(double y)
{
    return (*nrfunc)(xsav,y);

The capital variable K in function bbb is a global variable defined in main().

I just want to know what are the x and y values passed into function bbb.

FalloutRanger
  • 127
  • 2
  • 8
  • 4
    This code makes no sense, where is this K variable in bbb? You also dont show how aaa uses bbb – Borgleader Jun 01 '15 at 16:50
  • Yeah, this looks incomplete. Post the definition of aaa() function. Also, I'm betting there's a typedef somewhere that makes a pointer to the bbb() function. – rost0031 Jun 01 '15 at 16:51
  • I've posted it. Check it out!!! – FalloutRanger Jun 01 '15 at 17:07
  • @FalloutRanger check the answer I've posted. You are basically passing a pointer to function with some parameters, then pass the arguments separately. – vsoftco Jun 01 '15 at 17:14
  • @vsoftco Yeah I read your answer, but don't understand it. I'm pretty good at Fotran, but an absolute beginner at C. Not sure what you mean by "pass the parameters separately". – FalloutRanger Jun 01 '15 at 17:18
  • @FalloutRanger you first pass the pointer to function, call it `fptr`. This points to a function taking let's say one argument. You need to pass this argument, if you want to invoke the function via the pointer to function. So you pass the argument separately. In my example, `fptr` is the pointer to a function that takes an `int`, and `x` is what you'll invoke `fptr` with. In conclusion, the function `a` in my example takes 2 parameters: one is the pointer to function, the other is the parameter you want to call your pointer to function. – vsoftco Jun 01 '15 at 17:23
  • I now kinda understand it lol. But there is still a question: function bbb requires two variables (x and y), but in "double qgaus(double (*func)(double), double a, double b);" there is only one variable of type double required for function bbb. Why is that? Why when function bbb is passed into function qgaus, there is only one variable following it? – FalloutRanger Jun 01 '15 at 17:40
  • @FalloutRanger: `double qgaus(double (*func)(double), double a, double b);` is a declaration (inside a function in your case) of function. There are no call to `func` here (the parameter name are irrelevant and may be omitted here) – Jarod42 Jun 01 '15 at 18:45
  • @Jarod42 Now I understand. Thank you! – FalloutRanger Jun 01 '15 at 20:14

1 Answers1

2

Most probably (assuming the code compiles) aaa is a function that takes as its first parameter a pointer-to-function. It doesn't matter if you dereference the pointer-to-function, the call is still valid. So in your case *b is simply decaying to a function pointer, which presumably is used inside aaa (of which definition you don't provide). Simple example:

#include <iostream>

void f(int x)
{
    std::cout << "void f(int) invoked, with x = " << x << std::endl;
}

void a(void (*fptr)(int), int x)
{
    fptr(x); // call the function pointed by fptr with argument x
}

int main() 
{
    a(f, 10);
    a(*f, 20); // same as above
    a(****f, 42); // still the same
}

So in your code you first pass the pointer to function, then the arguments of the function, which are then used when calling the latter via the pointer-to-function.

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252