在 C 中,作为参数传递时,“&function”和“function”之间有什么区别?

发布于 2024-11-14 13:15:09 字数 788 浏览 2 评论 0原文

例如:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬心 2024-11-21 13:15:09

没有什么区别。有关证据,请参阅 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."

千寻… 2024-11-21 13:15:09

&function 和 function 之间没有区别 - 它们都是地址。您可以通过打印它们来看到这一点:

function bar(); 

.... 
printf("addr bar is 0x%d\n", &bar);
printf("bar is 0x%d\n", bar);

There is no difference between &function and function - they're both addresses. You can see this by printing them both:

function bar(); 

.... 
printf("addr bar is 0x%d\n", &bar);
printf("bar is 0x%d\n", bar);
独守阴晴ぅ圆缺 2024-11-21 13:15:09

作为争论传递时, &functionfunction 之间没有区别

,但是您的 typedef 之间存在差异。我不知道官方的解释,即到底有什么区别,但从我的记忆来看

typedef void (*name1)(void);

typedef void(name2)(void);

是不同的:

name1 是一个指向不带参数且不返回任何内容的函数的指针

name2 是一个不带参数且不返回任何内容的

函数通过编译来测试它:

typedef void (*pointer)(void);
typedef void (function)(void);

void foo(void){}

int main()
{
    pointer p;
    function f;

    p = foo; //compiles
    p();

    f = foo; //does not compile
    f();
}

同样,我不是解释这种行为的确切原因的合适人选,但我相信如果您查看标准,您会在那里找到解释

there is no difference between &function and function when passing as arguement

however 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

typedef void (*name1)(void);

and

typedef void(name2)(void);

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:

typedef void (*pointer)(void);
typedef void (function)(void);

void foo(void){}

int main()
{
    pointer p;
    function f;

    p = foo; //compiles
    p();

    f = foo; //does not compile
    f();
}

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

吃不饱 2024-11-21 13:15:09

区别只是风格上的。使用函数指针时,您会遇到相同的情况:

void func (void);

...

void(*func_ptr)(void) = func;

func_ptr();    // call func
(*func_ptr)(); // call func

printf("%d\n", ptr); 
printf("%d\n", *ptr);

有些人说 (*func_ptr)() 语法更可取,以明确函数调用是通过函数指针完成的。其他人认为带 * 的样式更清晰。

像往常一样,可能没有科学研究证明任何一种形式都比另一种更好,所以只需选择一种风格并坚持下去即可。

The difference is only stylistic. You have the same scenario when using function pointers:

void func (void);

...

void(*func_ptr)(void) = func;

func_ptr();    // call func
(*func_ptr)(); // call func

printf("%d\n", ptr); 
printf("%d\n", *ptr);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文