函数指针的解引用是如何发生的?

2022-01-31 00:00:00 pointers c function-pointers c++

为什么以及如何取消引用函数指针只是什么都不做"?

Why and how does dereferencing a function pointer just "do nothing"?

这就是我要说的:

#include<stdio.h>

void hello() { printf("hello"); }

int main(void) { 
    (*****hello)(); 
}

<小时>

来自这里的评论:

函数指针只是取消引用很好,但是结果函数指定人将立即转换回函数指针

function pointers dereference just fine, but the resulting function designator will be immediately converted back to a function pointer

<小时>

从 这里:

取消引用(以您认为的方式)函数的指针意味着:访问一个编码内存,因为它将是一个数据记忆.

Dereferencing (in way you think) a function's pointer means: accessing a CODE memory as it would be a DATA memory.

函数指针不应该是以这种方式取消引用.相反,它被调用.

Function pointer isn't suppose to be dereferenced in that way. Instead, it is called.

我会使用名称取消引用"端与呼叫"并排.没关系.

I would use a name "dereference" side by side with "call". It's OK.

无论如何:C 是这样设计的这两个函数名称标识符为以及变量保持函数的指针的含义相同:CODE的地址记忆.它允许跳转到那个通过使用 call() 语法来存储内存在标识符或变量上.

Anyway: C is designed in such a way that both function name identifier as well as variable holding function's pointer mean the same: address to CODE memory. And it allows to jump to that memory by using call () syntax either on an identifier or variable.

<小时>

究竟函数指针的解引用是如何工作的?


How exactly does dereferencing of a function pointer work?

推荐答案

这不是一个完全正确的问题.至少对于 C,正确的问题是

It's not quite the right question. For C, at least, the right question is

右值上下文中的函数值会发生什么?

What happens to a function value in an rvalue context?

(一个右值上下文是任何一个名字或其他引用出现的地方,它应该被用作一个值,而不是一个位置――基本上除了在赋值的左侧之外的任何地方.名字本身来自 右侧-作业的手边.)

(An rvalue context is anywhere a name or other reference appears where it should be used as a value, rather than a location ― basically anywhere except on the left-hand side of an assignment. The name itself comes from the right-hand side of an assignment.)

好的,那么在右值上下文中函数值会发生什么?它立即隐式转换为指向原始函数值的指针.如果您使用 * 取消引用该指针,您将再次获得相同的函数值,该值会立即隐式转换为指针.您可以根据需要多次执行此操作.

OK, so what happens to a function value in an rvalue context? It is immediately and implicitly converted to a pointer to the original function value. If you dereference that pointer with *, you get the same function value back again, which is immediately and implicitly converted into a pointer. And you can do this as many times as you like.

您可以尝试两个类似的实验:

Two similar experiments you can try:

  • 如果在 lvalue 上下文(赋值的左侧)中取消引用函数指针会发生什么.(答案将取决于您的期望,如果您记住函数是不可变的.)

  • What happens if you dereference a function pointer in an lvalue context―the left-hand side of an assignment. (The answer will be about what you expect, if you keep in mind that functions are immutable.)

在左值上下文中,数组值也被转换为指针,但它被转换为指向 element 类型的指针,而不是指向数组的指针.因此,取消引用它会给你一个元素,而不是一个数组,并且你表现出的疯狂不会发生.

An array value is also converted to a pointer in an lvalue context, but it is converted to a pointer to the element type, not to a pointer to the array. Dereferencing it will therefore give you an element, not an array, and the madness you show doesn't occur.

希望这会有所帮助.

附:至于为什么函数值会被隐式转换为指针,答案是对于我们这些使用函数指针的人来说,不用& 无处不在.还有双重便利:调用位置的函数指针会自动转换为函数值,因此您不必编写 * 来通过函数指针调用.

P.S. As to why a function value is implicitly converted to a pointer, the answer is that for those of us who use function pointers, it's a great convenience not to have to use &'s everywhere. There's a dual convenience as well: a function pointer in call position is automatically converted to a function value, so you don't have to write * to call through a function pointer.

附言与 C 函数不同,C++ 函数可以重载,我没有资格评论 C++ 中的语义是如何工作的.

P.P.S. Unlike C functions, C++ functions can be overloaded, and I'm not qualified to comment on how the semantics works in C++.

相关文章