Hatena::Groupzigorou

Hackadelic Memo RSSフィード

2008-05-21アストロンとマホトラのコンボ攻撃

Re:Cでのポインタの読み方

| 13:53 |  Re:Cでのポインタの読み方 - Hackadelic Memo を含むブックマーク はてなブックマーク -  Re:Cでのポインタの読み方 - Hackadelic Memo

これ非常に分かりやすい。

ルール

  • 後置演算子が優先。
    • 関数の()
    • 配列の[]
  • 前置はあとで。

要は、「後ろに何かあったら、後ろが優先」です。

演算子の英語での読み方

  • * pointer to ...
  • () function returning ...
  • [] array of ...
Cでのポインタの読み方

ってルールに沿って解釈して行く。読み解く例は元記事にたくさん書いてある。

演習問題があるので、実際にやってみる。

char *(*c[10])(int *p);

こんな感じかな。

  1. char *(*c[10])(int *p);
    • c is array of ...
  2. char *(*c[10])(int *p);
    • c is array of pointer to ...
  3. char *(*c[10])(int *p);
    • c is array of pointer to function returning ...
  4. char *(*c[10])(int *p);
    • c is array of pointer to function returning pointer to ...
  5. char *(*c[10])(int *p);
    • c is array of pointer to function returning pointer to char

つまり、変数cはchar型のポインタを返す関数ポインタの配列

int (*(*p)(int *a))[5];

  1. int (*(*p)(int *a))[5];
    • p is pointer to ...
  2. int (*(*p)(int *a))[5];
    • p is pointer to function returning ...
  3. int (*(*p)(int *a))[5];
    • p is pointer to function returning pointer to ...
  4. int (*(*p)(int *a))[5];
    • p is pointer to function returning pointer to array of ...
  5. int (*(*p)(int *a))[5];
    • p is pointer to function returning pointer to array of int

つまり、変数pはint型の配列ポインタを返す関数のポインタ

こんな感じかな。

まとめ

K&Rの巻末にBNFが載ってた気がするので、変数宣言部のみのルールを抽出してパーサー書けば、この言い回しのジェネレータ作れるねwww

kzyskzys2008/06/07 06:46K&R なら「5.12 複雑な宣言」にある dcl が「この言い回しのジェネレータ」にあたると思います。

ZIGOROuZIGOROu2008/06/17 15:24> id:kzysさん

こんな辺境の地までありがとうございます。
K&R 5.12っすか。読んだはずなんだけど頭からすっ飛んでました><
復習しまーす。

トラックバック - http://zigorou.g.hatena.ne.jp/ZIGOROu/20080521