这个不兼容的指针会引发问题吗?

huangapple go评论60阅读模式
英文:

Does this incompatible pointer raise any problems?

问题

我有以下的开关语句,其中函数指针被"返回":

void getExampleById(int l, int *a, int *b, double (**f) (double), double (**F) (double, double)) {
    
    switch(l) {
        case 1:
            *a = 0; *b = 5; *f = f1; *F = F1; break;
        case 2:
            *a = 0; *b = 10; *f = f2; *F = F2; break;
        default:
            printf("something went wrong\n");
    }
}

现在,f1 和 F1 是遵循"指针模板" double (**f) (double), double (**F) (double, double) 的函数。但是 f2 和 F2 是这样的函数:

double f2(double t) {

    return t;
}

double F2(double t) {

    return t;
}

当像 (*F)(2,2) 这样调用由 getExampleById 返回的函数指针时,是否会引发严重问题?第二个参数是否只是传递到无效的地方,一切是否正常?

英文:

I have the following switch case statement, in which function pointers are "returned":

void getExampleById(int l, int *a, int *b, double (**f) (double), double (**F) (double, double)) {
    
    switch(l) {
        case 1:
            *a = 0; *b = 5; *f = f1; *F = F1; break;
        case 2:
            *a = 0; *b = 10; *f = f2; *F = F2; break;
        default:
            printf("something went wrong\n");
    }
}

Now, f1 and F1 are functions that follow the "pointer template" double (**f) (double), double (**F) (double, double). But f2 and F2 are function like

double f2(double t) {

    return t;
}

double F2(double t) {

    return t;
}

Does this raise any serious problems when calling the function pointer returned by getExampleById like (*F)(2,2). Might the second argument just be passed into nothing and everything is fine?

答案1

得分: 1

以下是翻译好的内容:

在声明的函数指针类型和实际函数之间存在不匹配。

您已经声明了函数指针F的类型为double (**F)(double, double),这意味着它应该指向一个接受两个double参数并返回一个double的函数。但是您的函数F2的类型是double (*)(double),因为它接受一个double并返回一个double。这种不匹配可能导致未定义的行为。

如果您尝试在F在switch case中被分配为F2之后调用(*F)(2,2),则您正在将参数传递给一个(F2)不希望接收参数的函数。即使函数F2忽略第二个参数,这也不能使它正确或安全。在C中,这是未定义的行为,这意味着任何事情都可能发生。它可能在某些平台上工作,而在其他平台上崩溃,或者可能导致其他不稳定的行为。

为了避免这种情况,您应该确保函数签名与函数指针匹配。对于f2F2,正确的函数指针类型应该是double (*)(double)。如果您想要使用F来处理两个double,则应确保它可能指向的所有函数都接受两个double

如果您需要F有时是接受两个参数的函数,有时是接受一个参数的函数,您可能需要重新考虑您的设计,因为在C中这不能直接以安全的方式实现。

英文:

There's a mismatch between the declared function pointer types and the actual functions.

You've declared the function pointer F to be of type double (**F) (double, double), which means it should point to a function that takes two double parameters and returns a double. But your function F2 is of type double (*)(double) as it takes one double and returns a double. This mismatch might lead to undefined behavior.

If you try to call (*F)(2,2), after F was assigned F2 in the switch case, you are passing an argument to a function (F2) that's not expecting it. Even though the function F2 ignores the second argument, this does not make it correct or safe. It's undefined behavior in C, which means anything can happen. It could work on some platforms and crash on others, or it could cause some other erratic behavior.

To avoid this, you should ensure that the function signature matches with the function pointer. For f2 and F2, the correct function pointer type would be double (*)(double). If you want to use F with two doubles, you should ensure that all the functions it might point to accept two doubles.

If you need F to be a function that takes two parameters sometimes, and one parameter at other times, you might need to reconsider your design, as this is not directly achievable in C in a safe manner.

huangapple
  • 本文由 发表于 2023年6月11日 22:57:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定