typedef - How do you read C declarations? -


i have heard of methods, none of them have stuck. try avoid complex types in c , try break them component typedef.

i'm faced maintaining legacy code called 'three star programmer', , i'm having hard time reading of ***code[][].

how read complex c declarations?

this article explains relatively simple 7 rules let read c declaration, if find wanting or needing manually: http://www.ericgiguere.com/articles/reading-c-declarations.html

  1. find identifier. starting point. on piece of paper, write "declare identifier as".
  2. look right. if there nothing there, or there right parenthesis ")", goto step 4.
  3. you positioned either on array (left bracket) or function (left parenthesis) descriptor. there may sequence of these, ending either unmatched right parenthesis or end of declarator (a semicolon or "=" initialization). each such descriptor, reading left right:

    • if empty array "[]", write "array of"
    • if array size, write "array size of"
    • if function "()", write "function returning"

    stop @ unmatched parenthesis or end of declarator, whichever comes first.

  4. return starting position , left. if there nothing there, or there left parenthesis "(", goto step 6.
  5. you positioned on pointer descriptor, "*". there may sequence of these left, ending either unmatched left parenthesis "(" or start of declarator. reading right left, each pointer descriptor write "pointer to". stop @ unmatched parenthesis or start of declarator, whichever first.
  6. at point have either parenthesized expression or complete declarator. if have parenthesized expression, consider new starting point , return step 2.
  7. write down type specifier. stop.

if you're fine tool, second suggestion use program cdecl: http://gd.tuwien.ac.at/linuxcommand.org/man_pages/cdecl1.html


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -