gpt4 book ai didi

c - C 函数中的函数

转载 作者:太空狗 更新时间:2023-10-29 16:22:42 24 4
gpt4 key购买 nike

<分区>

我正在编写与此类似的代码:

#include <stdio.h>

double some_function( double x, double y)
{
double inner_function(double x)
{
// some code
return x*x;
}

double z;
z = inner_function(x);
return z+y;

}

int main(void)
{
printf("%f\n", some_function(2.0, 4.0));
return 0;
}

这在 GCC 中编译完美(没有警告)但在 ICC 中编译失败。

ICC 给出:

main.c(16): error: expected a ";"
{
^

main.c(21): warning #12: parsing restarts here after previous syntax error
double z;
^

main.c(22): error: identifier "z" is undefined
z = inner_function(x);
^

compilation aborted for main.c (code 2)

我做错了什么?

谢谢。

(edit) 抱歉这个糟糕的例子。在我的原始代码中,我有点需要做这些事情。我正在使用 GSL 数值积分器并且有类似的东西:

double stuff(double a, double b)
{
struct parameters
{
double a, b;
};

double f(double x, void * params)
{
struct parameters p = (struct parameters *) params;
double a = p->a, b = b->b;
return some_expression_involving(a,b,x);
}
struct parameters par = {a,b};

integrate(&f, &par);

}

我有很多具有这种结构的函数:它们是具有大量外部参数的函数集成的结果。并且实现数值积分的函数必须接收一个指向函数类型的指针:

double f(double x, void * par)

我真的很喜欢以这种方式嵌套函数,这样我的代码就不会因函数过多而膨胀。我希望我可以用 ICC 编译它以加快速度。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com