如何做一个函数指针的解引用发生的呢? [英] How does dereferencing of a function pointer happen?
问题描述
为什么和如何提领一个函数指针只是什么都不做?
Why and how does dereferencing a function pointer just "do nothing"?
这就是我所说的:
#include<stdio.h>
void hello() { printf("hello"); }
int main(void) {
(*****hello)();
}
来自<一个评论href=\"http://stackoverflow.com/questions/2669971/how-does-this-hello-world-program-work/2669996#2669996\">here:
函数指针刚提领
很好,但由此产生的功能
标志将立即
转换回一个函数指针
function pointers dereference just fine, but the resulting function designator will be immediately converted back to a function pointer
和来自一个答案<一个href=\"http://stackoverflow.com/questions/944479/how-come-pointer-to-a-function-be-called-without-dereferencing/944539#944539\">here:
解引用(在方式你觉得)一
函数的指针指:访问
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设计以这样的方式
这两个函数的名称标识符
还有变量保持功能的
指针指的是相同的:地址code
记忆。它允许跳转到
内存使用()调用语法无论是
上的标识符或变量
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?
(右边的值上下文是在任何地方的名称或其它的参考显示位置应该被用作一个值,而不是一个位置&MDASH;基本上任何位置,除了在赋值的左手侧的名称本身来源于<青霉>右键的赋值的 - 手侧)。
(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:
-
如果您会发生什么事提领一个函数指针的的左值的上下文中,MDASH;赋值语句的左侧。 (答案将是你所期望的,如果你要记住,函数是不可改变的。)
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。至于的为什么的一个函数值隐式转换为指针,答案是,对于那些使用谁使用函数指针,这是一个极大的方便不具备使用&安培;
的无处不在。有一个双重便利,以及:呼叫位置的函数指针被自动转换为函数值,这样你就不必写 *
来通过函数指针调用。
P.S. As to why a function value is implicitly converted to a pointer, the answer is that for those of use 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屋!