2010-09-21 134 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