c - 带元组的 typedef 如何工作?

标签 c typedef type-definition

我们在作业程序中得到了这个 typedef。作为一个程序员菜鸟,我以前没见过这样的东西。这是否意味着任何 DoubleFunction2D 实际上都是 (double, double) 的二元组?

程序:

类型定义:

typedef double (*DoubleFunction)    (double);      
typedef double (*DoubleFunction2D)  (double, double);    
typedef double (*DoubleFunction3D)  (double, double, double);

示例用法
(我的任务的 WIP 解决方案,尚未编译/测试。内部):

double expf2D(double x, double y)
 {
     double r = sqrt(pow(x,2) + pow(y,2));
     return my_expf(r);
 }

double DiskMonteCarloIntegrator(DoubleFunction2D f, double r1, double r2, int N)
{
    bool is_inside_ring(double x, double y){
        return is_inside_ellipse(x, y, r2/2, r2/2) && !(is_inside_ellipse(x, y, r1/2, r1/2));
    }
    int i=0;
    double x, y, sum = 0;
    while(i < N)
    {   
        x = RandomDouble(-1*r1, r1);
        y = RandomDouble(-1*r1, r1);
        if(is_inside_ring(x, y))
        {
            sum += f(x, y);
            i++;
        }
    }
    double avg = sum / N;
    double integral = avg * (pow(r2, 2) - pow(r1, 2)) * M_PI;
    return integral;
}

//extract
void main(int argc, char *argv[]){  
    DiskMonteCarloIntegrator(expf2D, 1.0, 2.0, 1000000);
}

最佳答案

这里没有元组(实际上,C编程语言中没有“元组”)


typedef double (*DoubleFunction)    (double);      

意味着 DoubleFunction 是一个指向接受double 并返回double 的函数的指针。


typedef double (*DoubleFunction2D)  (double, double);    

意味着 DoubleFunction2D 是一个指向函数的指针,该函数采用两个 double 值并返回一个 double


typedef double (*DoubleFunction3D)  (double, double, double);

意味着 DoubleFunction3D 是一个指向函数的指针,该函数采用三个 double 值并返回一个 double

关于c - 带元组的 typedef 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43184790/

相关文章:

c - 为什么 'typedef struct' 没有在 C 中成为可选的?

javascript - Jointjs 类型定义

c - 为什么我的 C 套接字文件传输(服务器/客户端)程序只能正确运行一次?

c++ - 三元运算符作为命令?

c++ - 关于C中的任务递归(Visual Studio 15)

c - 指针 typedef 结构在 C 中如何工作?

c - 可靠的 shellcode 测试

c++ - typedef 枚举的重新定义

go - 无法在给定类型的类型定义上调用方法