2012-11-06 41 views
15

我對C相當陌生,我試圖找出一些C聲明。有人可以幫我解釋這些C聲明的含義嗎?找出C聲明

double (*b)[n]; 
double (*c[n])(); 
double (*d())[n]; 

我很感謝幫助!

+8

轉到[螺旋....(http://c-faq.com/decl/spiral.anderson.html) –

+1

該鏈接實際上是真正有用的,謝謝! –

+0

+1 thaks詢問這個我有同樣的問題,當我正在研究內核代碼 –

回答

12
double (*b)[n]; 

b是指針n的陣列加倍

double (*c[n])(); 

c是n個指針的數組採取的參數未指定數量,並返回雙重

double (*d())[n]; 

d是功能一個函數獲取不定數量的參數並返回一個指向n個雙精度數組的指針

一般來說,爲了解析這些類型的聲明,採取以下方法。讓我們看看最後的聲明,例如

double (*d())[n]; 

d是做了什麼第一件事?它被稱爲with(),因此它是一個函數,它帶有未指定數量的參數和returnig ...結果是怎麼回事?它被解除引用(*),因此它是指向的指針。然後將結果編入索引,因此它是n的數組...剩下的是什麼?雙倍,因此雙打。用粗體讀出這些部分,你會得到你的答案。

讓我們來看看另一個例子

void (*(*f)(int)[n])(char) 

這裏,f爲先提領,因此它是指針 ...它然後用(int)的調用,因此功能回吐int和返回,結果然後索引爲[n],所以n的數組。結果再次被解除引用,所以指向。然後結果被調用(char),所以函數取char並返回(全部留空)void。因此,f是指向一個函數的指針,它取int並返回一個n指針數組,返回char和返回void的函數。

HTH

+0

@Joachim:啊,忘了這是C.固定 –

+0

感謝,在最後的解釋幫我undertand這更好! –

+0

@JoeCrawley:增加了另一個例子......更復雜的一個。 –

8

的基本規則解析C聲明是「由右至左,留下一對括號時向右跳內向外看」,即啓動嵌套最深的一對括號和然後努力向右看。從技術上講,你必須知道操作員的關聯性,但在大多數情況下它已經足夠好用了。

現在,讓我們應用此(簡化)規則,以你的問題:


double (*b)[n]; 
     ^

b是

double (*b)[n]; 
     ^

指針

double (*b)[n]; 
      ^^^ 

和陣列

double (*b)[n]; 
^^^^^^ 
雙打


double (*c[n])(); 
     ^^^^ 

c是

double (*c[n])(); 
     ^

指針的數組,以

double (*c[n])(); 
       ^^ 

功能

double (*c[n])(); 
^^^^^^ 

返回兩倍。


double (*d())[n]; 
     ^^^ 

d是一個函數

double (*d())[n]; 
     ^

返回指針到

double (*d())[n]; 
      ^^^ 

double (*d())[n]; 
^^^^^^ 

雙打的陣列


有上最* nixes發現一個整潔的實用工具,叫做CDECL,這需要一個C聲明字符串,並把它成自然語言句子。

1

有兩個重要的資源,瞭解「C胡言亂語」:

輸出cdecl.org的:

  • double (*c[n])():語法錯誤(n無效點擊這裏)
  • double (*c[])():c聲明爲指針數組函數返回雙
3

讓我們試試這個方法。

第一,你應該熟悉這三個符號:

1. * -- a pointer. 
2. [] -- an array. 
3.() -- a function.(notice: not parentheses)

我們以 「雙(* d())[N]」,例如,

的第一步是找出在聲明的標識符,標識符是變量的名稱,在這裏它是「d」。

(i) 
-- what is "d"? 
------------------------------------------------------------------------ 
look to the right side of the identifier, to see if there is a "[]" or a "()" : 
...d[]...: d is an array. 
...d()...: d is a function. 

if neither, look to the left side, to see if there is a "*" : 
...*d...: d is a pointer. 
------------------------------------------------------------------------

現在我們發現d是函數。 使用X來代替d(),則聲明變爲 「雙(* X)[N]」

(ii) 
-- what is "x"? 
------------------------------------------------------------------------ 
repeat (i), we find that x is a pointer. 
that means, d is a function returning a pointer. 
------------------------------------------------------------------------

使用y以取代* x時,則聲明變爲 「雙Y [N]」

(iii) 
-- what is "y"? 
------------------------------------------------------------------------ 
repeat (i), we find that y is an array of n elements. 
that means, d is a function returning a pointer to an array of n elements. 
------------------------------------------------------------------------

使用Z到替換Y [N],則聲明變爲 「雙Z」

(iv) 
-- what is "z"? 
------------------------------------------------------------------------ 
repeat (i), we find that z is a double. 
that means, d is a function returning a pointer to an array of n double elements. 
------------------------------------------------------------------------

讓我們看看另一種表達:

void (*(*f)(int)[n])(char)
1. 
    we find f. 
2. 
    f is a pointer. *f -> a 
    void (*a(int)[n])(char) 
3. 
    a is a function. a() -> b 
    void (*b[n])(char) 
    --f is a pointer to a function (with an int parameter)-- 
4. 
    b is an array. b[] -> c 
    void (*c)(char) 
    --f is a pointer to a function returning an array (of n elements)-- 
5. 
    c is a pointer. *c -> d 
    void d(char) 
    --f is a pointer to a function returning an array of n pointers-- 
6. 
    d is a function returning void. 
    --f is a pointer to a function returning an array of n pointers to functions (with a char parameter) returning void--