函数指针"从兼容的指针类型&QUOT分配;只有在使用可变参数的省略号 [英] Function pointer "assignment from incompatible pointer type" only when using vararg ellipsis

查看:176
本文介绍了函数指针"从兼容的指针类型&QUOT分配;只有在使用可变参数的省略号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,声明一个函数(或函数指针),没有参数列表(和不指定无效在参数表中),这意味着该函数(或函数指针)有一个参数数目未知。

I know that declaring a function (or function pointer) with no parameter list (and without specifying void in the parameter list) that means that the function (or function pointer) has an unknown number of arguments.

我写了一些测试脚本来检查这一行为的:

I wrote some test scripts to check this behavior out:

int my_func_1() {
    return(0);
}
int my_func_2(int x) {
    return(x);
}
int my_func_3(char x, ...) {
    va_list va;
    va_start(va, x);
    return(va_arg(va, int));
}


int (*fp)();

fp = my_func_1;
printf("%d\n", fp());

fp = my_func_2;
printf("%d\n", fp(33));

fp = my_func_3;
printf("%d\n", fp(33, 44));

我已经在linux下编制了64位的机器上,例如:

Which I've compiled on a 64 bit machine under linux as such:

gcc test.c -Wextra -pedantic -std=c1x -O3 -o test

输出是正确的:

0
33
44

不过,我得到这样的警告:从兼容的指针类型 分配。如何来使用可变参数的省略号时,这个警告只显示了?

But I get this warning: assignment from incompatible pointer type. How come this warning only shows up when using the vararg ellipsis?

由于指针类型被视为不完整分配一个完整的类型不应该构成一个从兼容的指针类型分配。此外,指定品种齐全没有任何警告的作品,只要可变参数省略号不是present

Since the pointer type is considered incomplete assigning it a complete type should not constitute an "assignment from incompatible pointer type". Moreover, assigning complete types works without any warnings as long as the vararg ellipsis is not present

这<一个href=\"http://stackoverflow.com/questions/7308449/how-to-use-varargs-in-conjunction-with-function-pointers-in-c-on-win64\">other问题问几乎同样的事情。这些问题的答案都以它不工作方式,没有明确的参照标准,为什么它不会工作,这样就行了。

This other question asks nearly the same thing. The answers are all in the line of "it doesn't work that way" with no clear reference to the standard as to why it wouldn't work that way.


  • 为什么编译器生成的警告?

  • 这是行为(警告除外)可靠吗?

  • 这是行为具体的编译器(读看来MinGW的x86_64的不支持它在2011年的另一个问题)?

  • 这种行为是特定于平台的?

推荐答案

查看C标准的6.7.5.3:

See 6.7.5.3 of the C Standard:

此外,参数类型列表中,如果两者都present,应同意
  在的参数和所使用的省略号终止子的数目;
  相应参数应具有兼容的类型。如果一个类型都有
  一个参数类型列表及另一种类型是由函数指定
  说明符这不是一个函数定义的一部分,并且包含
  一个空的标识符列表,参数列表中不应有一个
  省略号终结和每个参数的类型必须兼容
  与从默认的应用程序产生的类型
  参数提升。

Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types. If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions.

这说的可变参数函数来FP分配具有不兼容的类型。

This says that the assignment of the varargs function to fp has incompatible types.

此外,正如我在评论中所指出上述情况,计划不确定的行为按定义是不可靠的...它可以在一个执行的一个版本工作在一个平台上,但在其他版本中,实现和平台的失败​​。 ..它甚至可以工作一天,但不是下一个,或者在程序的一部分,但不是另一个。务实,在一些实现可变参数和非可变参数的函数的调用顺序是不同的,你的计划很可能当这样一个实现运行崩溃。

In addition, as I noted in my comments above, the program has undefined behavior which by definition is unreliable ... it may work in one version of one implementation on one platform but fail in other versions, implementations, and platforms ... it might even work one day but not the next, or in one part of a program but not another. Pragmatically, in some implementations the calling sequences of varargs and non-varargs functions are different, and your program is likely to crash when run on such an implementation.

这篇关于函数指针&QUOT;从兼容的指针类型&QUOT分配;只有在使用可变参数的省略号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆