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

问题描述

int *(crack)();         // defining a function which return a pointer integer
int (*crack());         // defining a function which return a pointer integer
int *(*crack2)();       // defining a variable that will hold the address of a function. the function return type is pointer integer
int (*crack3)();        // defining a variable that will hold the address of a function. the function return type is integer
int ((*(crack5))());    // defining a variable that will hold the address of a function. the function return type is integer
int *(crack4());        // defining a function which return a pointer integer
int (*(*crack6)());     // defining a variable that will hold the address of a function. the function return type is pointer integer
int ((*(crack8)()));    // defining a variable that will hold the address of a function. the function return type is integer
int *(*((crack9)()));   // defining a function which return a pointer of pointer integer i.e. int **crack9()
int (*((*(cast()))));
int (*whatever(int justaval))(int param1, int param2); //define pointer of function which return pointer of function // i am exhausted 




我很难区分这两个东西.我知道哪一个是什么,但是当我试图使函数,变量和函数的指针变量之间有所区别时,生活变得越来越艰难.

所以,我的问题是,在哪里可以找到一个通用的定义或一组规则,这些规则或规则集将指导我编写一条通用的规则来区分这些




I am having a really hard time to differ between this things. I know which one is what but life is getting hell while I am trying to make a general rule to differ between variable, function and pointer variable of functions.

So, my question is where can I find a general definition or set of rules that would guide me to write a general rule to differ between these

推荐答案


int ((*(crack5))()); 


从crack5开始.删除第一组括号,因为它们什么也不做.然后我们离开了:


Start at crack5. Peel off the first set of parentheses, as they do nothing. Then we have left:

int ((*crack5)()); 


我们现在唯一能做的就是取消对指针操作的引用.因此,crack5是某种指针.
然后,在下一步中,我们唯一可以做的就是执行一个函数调用.因此,crack5必须是函数的指针.
再次剥离一组多余的括号,我们看到该函数返回一个int.

习惯这种读取声明需要花费一些时间.我建议您深入了解C ++优先规则,因为它们是所有这些的关键.


The only thing we can do now is dereference the pointer operation. Hence crack5 is some kind of a pointer.
Then in the next step, the only thing we can do is perform a function call. Hence crack5 must be a pointer to a function.
Peel off one redundant set of parentheses again and we see that the function returns an int.

It takes some time to get used to this kind of reading a declaration. I would recommend you take a deeeeep look at the C++ precedence rules, as they are the key to all this.


typedef int (*TReturnedFuncPtr)(int param1, int param2);
typedef TReturnedFuncPtr (*TOtherFuncPtr)(void* param);
TReturnedFuncPtr Implementation(void* param)
{
    ...
}


在高质量的C ++代码中,只有在与C代码(包括操作系统API)合作的情况下,您才看不到函数指针,但即使在那些情况下,通常也只涉及单个函数指针.如果您在C程序中看到类似于示例最后一行的代码,则可以肯定它是由初学者或魔术师手工制作的.如果您在C ++代码中看到的内容与它的内感和对OOP的缺乏理解相同.

如果您被迫读取此类丑陋的声明:nv3已经指出:读取函数指针声明最简单,方法是从函数指针类型的名称的中间开始.在我看来,允许这样丑陋的声明(例如,在一个声明中允许更多func ptr声明)以及将类型的名称保留在声明的中间都是C语言的错误决定.

不要在解密这样的声明上浪费时间.通常,质量不好或不好的代码都不会包含这样的声明.如果您发现一些丑陋的代码,那么每5年解密一次特定的声明就可以了.仅在被强迫时才利用您的时间来获得真正的知识,并浪费时间来学习这些东西.进行很多C/C ++编程而不遇到这种事情是一件好事.


In good quality C++ code you will never see function pointers, only in case of cooperation with C code (including the operating system APIs) but even in those cases usually only a single function pointer is involved. If you see code like the last line of your example in a C program then its pretty sure that it has been handcrafted by a beginner or a magician. If you see the same in C++ code that its guilt and lack of understanding of OOP.

If you are forced to read such ugly declarations: nv3 has already pointed it out: Reading function pointer declarations is easiest by starting out from in the middle from the name of the function pointer type. In my opinion both allowing such ugly declarations (for example allowing more func ptr declarations inside one declaration) and keeping the name of the type in the middle of the declaration were bad decisions in the C language.

Don''t waste your time on deciphering such declarations. Usually neither bad nor good quality code contains declarations like that. Its fine to decipher one specific declaration once in 5 years if you find some ugly piece of code. Use your time to gain true knowledge and waste your time on learning these things only if you are forced to. Its only a good thing that you did a lot of C/C++ programming without encountering such things.


这篇关于函数定义和函数指针变量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 03:31