在C中,作为参数传递时,`& function`和`function`有什么区别? [英] In C, what is the difference between `&function` and `function` when passed as arguments?
本文介绍了在C中,作为参数传递时,`& function`和`function`有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
#include
typedef void(* proto_1)();
typedef void proto_2();
void my_function(int j){
printf(hello from function。我得到%d.\\\
,j);
}
void call_arg_1(proto_1 arg){
arg(5);
}
void call_arg_2(proto_2 arg){
arg(5);
}
void main(){
call_arg_1(& my_function);
call_arg_1(my_function);
call_arg_2(& my_function);
call_arg_2(my_function);
}
运行这个我得到以下内容:
> tcc -run try.c
来自函数的hello。我从函数中获得了5.
hello。我从函数中获得了5.
hello。我从函数中获得了5.
hello。我得到了5.
我的两个问题是:
(* proto)
定义的函数原型和没有定义的函数原型之间的区别是什么?
&
)而不用?
$ b $之间有什么区别b
解决方案
没有区别。有关证据,请参阅 C99规范(第6.7节。
$ b
参数声明为''函数返回类型''应调整为''指向
函数的指针返回类型'',如6.3.2.1。
For example:
#include <stdio.h>
typedef void (* proto_1)();
typedef void proto_2();
void my_function(int j){
printf("hello from function. I got %d.\n",j);
}
void call_arg_1(proto_1 arg){
arg(5);
}
void call_arg_2(proto_2 arg){
arg(5);
}
void main(){
call_arg_1(&my_function);
call_arg_1(my_function);
call_arg_2(&my_function);
call_arg_2(my_function);
}
Running this I get the following:
> tcc -run try.c
hello from function. I got 5.
hello from function. I got 5.
hello from function. I got 5.
hello from function. I got 5.
My two questions are:
- What is the difference between a function prototype defined with
(* proto)
and one defined without? - What is the difference between calling a function with the reference operator (
&
) and without?
解决方案
There is no difference. For evidence see the C99 specification (section 6.7.5.3.8).
"A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1."
这篇关于在C中,作为参数传递时,`& function`和`function`有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文