在 C 中,作为参数传递时,`&function` 和 `function` 之间有什么区别? [英] In C, what is the difference between `&function` and `function` when passed as arguments?

查看:16
本文介绍了在 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.
",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.

我的两个问题是:

  • (* proto) 定义的函数原型和没有定义的函数原型有什么区别?
  • 使用引用运算符 (&) 和不使用引用运算符调用函数有什么区别?
  • 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?

推荐答案

没有区别.有关证据,请参阅 C99 规范(第 6.7 节.5.3.8).

There is no difference. For evidence see the C99 specification (section 6.7.5.3.8).

"将参数声明为‘函数返回类型’应调整为‘指向函数返回类型'',如6.3.2.1."

"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 中,作为参数传递时,`&amp;function` 和 `function` 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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