函数指针的解除引用是如何发生的? [英] How does dereferencing of a function pointer happen?

查看:28
本文介绍了函数指针的解除引用是如何发生的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<小时>

从答案这里:

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

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 就是这样设计的这两个函数名称标识符都为以及变量保持函数的指针的意思相同:指向代码的地址记忆.它允许跳到那个通过使用 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.

<小时>

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

推荐答案

这不是一个完全正确的问题.至少对于 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.)

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

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.

P.P.S.与 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++.

这篇关于函数指针的解除引用是如何发生的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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