关于c函数指针的2个问题 [英] 2 questions about function pointer in c
问题描述
我有两个问题。
1.我看到
int (*Ptr)(int,int);
Ptr=someOtherFuncion;
它不应该是这样的?
Its not should be like that?
Ptr=&someOtherFuncion;
2.我知道函数指针的回调
像
2.I learn that function pointer its for callback like that
someOtherFunction(functionPointer)
是什么产生差异,如果我会把未指针常规的函数?
what is the differeence if I will put a regular function that is not pointer?
推荐答案
函数的名称几乎立刻衰变到一个函数指针,所以 someOtherFunction
衰减到该同一指针和放大器; someOtherFunction
为您提供了明确。事实上,操作数的地址运算符(&安培;
)是少数地方之一是衰减的不的发生
The name of a function decays almost immediately to a pointer to the function, so someOtherFunction
decays to the same pointer that &someOtherFunction
gives you explicitly. In fact, the operand of the address-of operator (&
) is one of the few places were the decay doesn't happen.
这有有趣的结果:即使你取消引用函数指针,它再次衰减的时候了。所以下面都是等价的:
This has amusing consequences: Even if you dereference the function pointer, it decays again right away. So the following are all equivalent:
someOtherFunction(1, 2);
(*someOtherFunction)(1, 2);
(**someOtherFunction)(1, 2);
(***someOtherFunction)(1, 2);
所以,如果你感觉不适分配给一个函数指针没有明确的地址的,通过各种手段把&安培;
在那里,但你没有到
要解决这个问题的第二部分:一个功能的总是的通过函数指针调用,但是因为上面提到的瞬间衰减的,正常的功能可以被称为只是方式与功能相同指针。
To address the second part of the question: A function is always called through a function pointer, but because of the above-mentioned instant decay, normal functions can be called just the same way as function pointers.
这篇关于关于c函数指针的2个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!