函数返回的指针和指向函数的指针之间有什么区别 [英] what difference between function returning pointer and pointer to function

查看:95
本文介绍了函数返回的指针和指向函数的指针之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题,直接回答了函数返回指针和指向函数的指针之间的区别是什么.一个简单的示例并附有详细的解释(以一般人的说法)将对您有所帮助或提供教程链接. 谢谢
anoop

well a simple question and straight forward one whats difference between function returning pointer and pointer to function. a simple example with detail explanation(in lay men terms) would be helpful or tutorial link .
thanks
anoop

推荐答案

这两者之间有着天壤之别.

返回指针的函数就是一个函数,也就是说,它是执行某些操作并返回指向某些数据(或可能指向另一个函数)的指针变量的代码块.

指向函数的指针是可以在程序执行期间设置的变量,以指向不同的函数,并允许通过引用指针变量间接调用那些函数.

但是不要以为两者之间不能互换,指针不是函数,反之亦然.
There is a world of difference between these two.

A function that returns a pointer is a function, that is to say, it is a block of code that performs some actions and returns a pointer variable that points to some data (or maybe to another function).

A pointer to a function is a variable that can be set during program execution to point to different functions and allow those functions to be called indirectly, that is by reference to the pointer variable.

But do not think that the two are in any way interchangeable, a pointer is not a function and vice versa.


1.好吧,返回指针的函数"是返回值是指针的函数,即它返回一个内存地址.这通常很有用,例如考虑 malloc [ ^ ]:调用方要求一个内存缓冲区(指定其大小)并获取新分配的内存块的起始地址(指针).
另一个示例是函数 strchr [变量.对于间接调用函数很有用.例如,假设您具有以下代码:

1. A ''function returning a pointer'' is, well, a function whose return value is pointer, i.e. it returns a memory address. This is often useful, for instance consider malloc[^]: the caller asks for a memory buffer (specifying its size) and gets back the starting address (the pointer) of a freshly allocated memory block.
Another example is the function strchr[^] that returns the address of first occurrence of a character inside a given string.

2. A ''pointer to function'' is a variable holding the address of a function (i.e. a pointer to code memory). It is useful for indirectly calling functions. For instance, suppose you have the following code:

switch (op)
  case 0:
    return sum(x,y);
  case 1:
    return sub(x,y);
  case 2:
    return mul(x,y);
  case 3: 
   return  div(x,y);
  //...



使用函数指针数组,您可以改为编写以下单行:



with an array of function pointers, you might write instead the following single line:

return pfun[op](x,y);



回调机制是函数指针有用的另一种情况.例如,请参阅Windows API函数
EnumWindows [ ^ ]或 CreateThread [ ^ ].



Callback mechanism is another scenario where function pointers are useful. See for instance the Windows API functions EnumWindows[^] or CreateThread[^].


请参阅我对问题的评论,并尝试以我们知道要解释的方式重新构建问题.

要理解错误问题是什么意思,请参见:
类与封装之间的区别是什么编程 [ ^ ];
讨论内容:如何提出一个好问题? [ ^ ].

现在,我注意到了另一个歧义:第二种选择可能是函数返回指向(另一个)函数的指针"或函数返回指针"(某种类型,未指定).

您将无法找到准确回答您问题的手册,仅是因为您问了无限数量的变体中只有两种(不是精确制定的)情况.因此,您唯一的手册应该是一些通用的C或C ++手册或书籍,并阅读其中的任何内容.首先,指针;然后是功能,其签名,声明和使用;最后是函数指针,声明类型(语法)及其用法.

并且,当您结合这些知识时,您可以再次以更具体的方式提出问题. 这是我的声明;我希望如此,但是……"-类似的东西.
我认为这是唯一可以真正帮助您的东西.

—SA
Please see my comments to the question and try to re-formulate the question the way we would know what to explain.

To understand what do I mean by incorrect question, please see also:
what is the difference between the class and encapsulation in programming[^];
and this discussion: How to ask a good question?[^].

Now, I noticed another ambiguity: the second alternative could be a "function returning a pointer to (another) function" or "function returning a pointer" (of some type, unspecified).

You won''t be able to find a manual answering exactly your question, just because you ask about just two (not precisely formulated) situation out of unlimited number of variants. So, your only manual should be some general C or C++ manual or book and read something which is explained in any of them. First, pointers; then, functions, their signatures, declarations and use; and, finally, function pointers, declaring types (syntax) and their use.

And when you combine this knowledge, you can ask your question again, this time in a more specific way. "This is my declaration; I expect this, but…" — something like that.
I think this is the only thing which can really help you.

—SA


这篇关于函数返回的指针和指向函数的指针之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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