2010-09-21 90 views
6

我有一个问题,在某种程度上,我猜,完全微不足道:那是什么(以及为什么)?晦涩的指针声明

const float *(*const*)(int) 

我的理解是,这是一个“指针到恒定指向函数以一个int作为参数,返回一个指针,以恒定的浮动”。

它是正确的吗?如何“精神分析”(*const*)?特别是因为没有名字,起初我不知道从哪里开始。我认为“名称”的唯一可能性是这样的:*const *name作为其他组合是无效的(如果我是正确的),那么“名称是一个指针常量指针...”。

这个推理是否有效?

谢谢!

+4

我尝试通过使用typedef将它分解成组件来避免这种令人费解的定义。 – 2010-09-21 21:31:47

+1

c iggerish to EN:“http://cdecl.ridiculousfish.com/?q=const+float+*%28*const*+foo%29%28int%29” – Anycorn 2010-09-21 21:33:12

+0

对于有困难的人将上面的代码插入编译器(const float *(* const *)(int));'这与'int foo(const float *(* const * var_name)(int)')是一样的。 ;' – nategoose 2010-09-21 21:36:10

回答

6

是的推理是有效的。将它放在不在函数参数列表中的所有'*'es的右侧,并且不在函数参数列表中的所有[]'es的左侧。然后你有

const float *(*const* name)(int) 

然后像往常一样阅读。从上面,甚至如何找到正确的名称的地方,there are许多教程如何解析这个在互联网上。

对于C++,您可能需要查看geordi

< litb> geordi: -c int name; 
< geordi> Success 
< litb> geordi: make name a const float *(*const*)(int) and show 
< geordi> -c float const *(*const* name)(int) ; 
< litb> geordi: << TYPE_DESC(const float *(*const*)(int)) 
< geordi> pointer to a constant pointer to a function taking an integer 
      and returning a pointer to a constant float 

然后,您可以做分析,在它的

< litb> geordi: show parameter-declaration and first decl-specifier-seq 
< geordi> `int` and `float const`. 
< litb> geordi: make name an array of 2 pointer to function taking int and 
     returning pointer to float and show 
< geordi> -c float const *(* name[2])(int); 

它可以让你用英语

< litb> geordi: make name an array of 2 pointer to (float const*(int)) and show 
< geordi> -c const float *(* name[2])(int); 

正如你看到的混合物C的语法,这是相当强大的。

+0

感谢链接到“geordi”,它看起来令人印象深刻! – 2010-09-22 07:06:30

12

你的名字是正确的*const*。如果您在cdecl.org插线const float *(*const* name)(int),它会告诉你,它的意思是“申报名称为指向const的函数指针(INT)返回指针为const浮动

至于心理分析,我只记得R (*p)(A)是一个指向函数的指针。因此R (**p)(A)是指向函数指针的指针,此时所需要的全部是记住const*如何进行交互。

+1

+1教我关于cdecl.org – nategoose 2010-09-21 21:33:55

+1

++关于链接到cdecl.org – AlcubierreDrive 2010-09-21 21:34:00

+0

感谢您的cdecl链接。 – 2010-09-22 07:07:45

2

刚从外面工作

你有形式的东西:

U D(V) 

让你有喜欢的东西的功能。

东西 - 某些东西是以int和返回const float*为函数。

对于(*const*)你向后工作得到const在正确的位置(虽然是对称的,甚至没有在这里!)。 “指向const指针的指针”,就像int * const * p中的(说)p是一个指向const指针的指针,指向int

把它放在一起:指向函数的常量指针的指针取int并返回const float *

+0

谢谢!这并不复杂。 – 2010-09-22 07:04:51