在 C 中,作为参数传递时,“&function”和“function”之间有什么区别?
例如:
#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);
}
运行此程序,我得到以下信息:
> 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.
我的两个问题是:
- 使用
(* proto)
定义的函数原型和不使用(* proto)
定义的函数原型之间有什么区别? - 使用引用运算符 (
&
) 调用函数与不使用引用运算符调用函数有什么区别?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有什么区别。有关证据,请参阅 C99 规范(第 6.7 节) .5.3.8)。
“将参数声明为“函数返回类型”应调整为“指向
函数返回类型'',如 6.3.2.1 中所示。”
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."
&function 和 function 之间没有区别 - 它们都是地址。您可以通过打印它们来看到这一点:
There is no difference between &function and function - they're both addresses. You can see this by printing them both:
作为争论传递时,
&function
和function
之间没有区别,但是您的 typedef 之间存在差异。我不知道官方的解释,即到底有什么区别,但从我的记忆来看
,
是不同的:
name1 是一个指向不带参数且不返回任何内容的函数的指针
name2 是一个不带参数且不返回任何内容的
函数通过编译来测试它:
同样,我不是解释这种行为的确切原因的合适人选,但我相信如果您查看标准,您会在那里找到解释
there is no difference between
&function
andfunction
when passing as arguementhowever there is a difference between your typedefs. I do not know the official explanation, i.e what exactly the difference, but from what i remember
and
are different:
name1 is a pointer to a function that takes no paramter and returns nothing
name2 is a function that takes no paramter and returns nothing
you can test it by compiling:
again, i am not the right person to explain exact reason of this behavior but i believe if you take a look at standards you will find the explanation somewhere there
区别只是风格上的。使用函数指针时,您会遇到相同的情况:
...
有些人说 (*func_ptr)() 语法更可取,以明确函数调用是通过函数指针完成的。其他人认为带 * 的样式更清晰。
像往常一样,可能没有科学研究证明任何一种形式都比另一种更好,所以只需选择一种风格并坚持下去即可。
The difference is only stylistic. You have the same scenario when using function pointers:
...
There are some who say that the (*func_ptr)() syntax is to prefer, to make it clear that the function call is done through a function pointer. Others believe that the style with the * is clearer.
As usual, there are likely no scientific studies proving that either form is better than the other, so just pick one style and stick to it.