我如何理解这个C类型声明? [英] How can I make sense of this C type declaration?
问题描述
double (*bar(int, double(*)(double,double[])))(double);
在审查演讲幻灯片时,我发现有一个练习留给学生:
While reviewing a lecture slide, I found an exercise left to the student:
用简单的英语来说,此C声明中的
bar
是什么类型?
请帮助我完成此过程。我什至不知道从哪里开始,除了最终会返回双精度值。
Please help walk me through this. I don't even know where to begin, except that something is ultimately returning a double.
推荐答案
此答案带给您使用螺旋规则的能力。能够通过从未知元素开始并对其进行阅读来理解复杂的表达式(首先解决括号中的问题)。
This answer is brought to you by the ability to use the Spiral Rule. Being able to understand a complex expression by starting at the unknown element and reading around it (resolving things in the parenthesis first). A very useful skill when reading code.
bar - bar
bar() - is a function
bar(int, ) - which takes an int...
bar(int, (*)()) - and a function pointer
bar(int, double(*)()) - which returns a double
bar(int, double(*)(double, )) - and takes a double...
bar(int, double(*)(double, double[])) - and an array of doubles
(*bar(int, double(*)(double, double[]))) - and returns a pointer
(*bar(int, double(*)(double, double[])))() - to a function
(*bar(int, double(*)(double, double[])))(double) - taking a double
double(*bar(int, double(*)(double, double[])))(double) - which returns a double
那是har d方法...当然,有些站点使此操作变得容易,例如, cdecl 网站;但是即使您无法上网也能够读取代码。
That was the hard way... There are of course sites that make this easier, the cdecl site for example; but it's good to be able to read code even when you can't get to the internet.
这篇关于我如何理解这个C类型声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!