简体   繁体   English

关于Pointers To函数声明中的函数

[英]About Pointers To Functions in function declarations

#include<stdio.h>
#include<stdlib.h>

int fun1()
{
    printf("I am fun1.");
    return 0;
}

int fun2(int fun())
{
    fun();
    return 0;
}

int main()
{
    fun2(fun1);
    return 0;
}

The above program can run. 上面的程序可以运行。 As far as I am concerned, I can understand int fun2(int (*fun)()) , but I do not know how int fun2(int fun()) works. 就我而言,我可以理解int fun2(int (*fun)()) ,但我不知道int fun2(int fun())工作的。 Thank you. 谢谢。

When you write int fun2(int fun()) , the parameter int fun() converts into int (*fun)() , it becomes exactly equivalent to this: 当你编写int fun2(int fun()) ,参数int fun()转换为int (*fun)() ,它变得完全等同于:

int fun2(int (*fun)());

A more famiiar conversion happens in case of array when you declare it as function parameter. 当您将其声明为函数参数时,在数组的情况下会发生更多的famiiar转换。 For example, if you've this: 例如,如果你这样:

int f(int a[100]);

Even here the parameter type converts into int* , and it becomes this: 即使在这里参数类型转换为int* ,它变为:

int f(int *a);

The reason why function type and array type converts into function pointer type, and pointer type, respectively, is because the Standard doesn't allow function and array to be passed to a function, neither can you return function and array from a function. 函数类型和数组类型分别转换为函数指针类型和指针类型的原因是因为标准不允许将函数和数组传递给函数,也不能从函数返回函数和数组。 In both cases, they decay into their pointer version. 在这两种情况下,它们都会衰减为指针版本。

The C++03 Standard says in §13.1/3 (and it is same in C++11 also), C ++ 03标准在§13.1/ 3中说(在C ++ 11中也是如此),

Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent . 仅在那一个中​​不同的参数声明是函数类型而另一个是指向相同函数类型的指针是等效的 That is, the function type is adjusted to become a pointer to function type (8.3.5) . 也就是说,调整函数类型以成为函数类型的指针(8.3.5)

And a more interesting discussion is here: 这里有一个更有趣的讨论:

int fun2(int (*fun)()) and int fun2(int fun()) are exactly the same. int fun2(int (*fun)())int fun2(int fun())完全相同。 When you declare a function argument from a function type, the compiler uses it as if it were a pointer to the same function type. 当您从函数类型声明函数参数时,编译器会将其用作指向同一函数类型的指针。

These two function definitions are equivalent in C: 这两个函数定义在C中是等价的:

 int fun2(int fun()) { ... }

and

 int fun2(int (*fun)()) { ... }

In the first function the parameter is adjusted to a function pointer. 在第一个函数中,参数被调整为函数指针。 See C Standard paragraph: 见C标准段落:

(C99, 6.7.5.3p8) "A declaration of a parameter as ''function returning type'' shall be adjusted to ''pointer to function returning type'', as in 6.3.2.1." (C99,6.7.5.3p8)“参数声明为''函数返回类型''应调整为''函数返回类型的指针'',如6.3.2.1所述。”

Looking to it in a lower level (and in a x86-based architecture): 在较低级别(以及基于x86的架构)中查看它:

int fun2(int fun())

int fun() 's address is pushed into the stack and passed to fun2() function. int fun()的地址被推入堆栈并传递给fun2()函数。

int fun2(int (*fun)())

int fun() 's pointer address is pushed into the stack and passed to fun2() function. int fun()指针地址被推入堆栈并传递给fun2()函数。

The result is the same, except that with second one, you pass the fun()'s address by reference, and in the first one you pass it by value. 结果是一样的,除了第二个,你通过引用传递fun()的地址,并在第一个中通过值传递它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM