c++ - 函数内的函数不带括号?

标签 c++ c function variables parentheses

有这样的C代码:

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

aaa 是一个函数,bbb 是另一个函数:

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

我的问题是,当函数aaa内调用bbb时,bbb后面没有任何括号,即没有变量传递到函数bbb中。那么函数bbb中x和y的值是多少呢?都为零?

好吧,这是一段很长的代码的一部分:

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

aaa及相关函数:

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

函数bbb中的大写变量K是main()中定义的全局变量。

我只想知道传递给函数 bbb 的 x 和 y 值是多少。

最佳答案

最有可能(假设代码编译)aaa 是一个将函数指针作为其第一个参数的函数。如果取消引用函数指针 the call is still valid 也没关系。 。因此,在您的情况下, *b 只是衰减为函数指针,该指针可能在 aaa 内部使用(您未提供其定义)。简单的例子:

#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
}

因此,在代码中,您首先传递函数指针,然后传递函数的参数,然后在通过函数指针调用后者时使用这些参数。

关于c++ - 函数内的函数不带括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30578918/

相关文章:

c - 为什么BSD queue.h LIST 叫做列表?

c - 词法分析器中的奇怪数字

c++ - 相同功能的不同实现(c/c++)

python - 无法为 "high"x 计算 e^(-x)

c++ - vector 迭代器不可取消引用(尝试手动反转 vector )

一个项目中的 C++14 和 C++17

c++ - 为什么 std::unique_ptr 需要专门用于动态数组?

c++ - C++ 模板的声明和定义分离

C++Builder编译问题

javascript - 如何从构造函数中调用方法