complex c declarations

Upload: maheshwar-bhat

Post on 02-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Complex C Declarations

    1/3

    Princeton University

    COS 217: Introduction to Programming Systems

    Complex C Declarations

    Te document so!n "elo! is retrieved #rom tis site: ttp:$$ieng%&ucsd&edu$'cs()x$rt*lt&rule&tml& Itdescri"es te +rigt,le#t rule-+ a tecni.ue #or understanding and composing complex C declarations&

    The "right-left" rule is a completely regular rule for deciphering Cdeclarations. It can also be useful in creating them.

    First, symbols. Read

    * as "pointer to" - always on the left side ! as "array of" - always on the right side # as "function returning" - always on the right side

    as you encounter them in the declaration.

    $T%& '------

    Find the identifier. This is your starting point. Then say to yourself,"identifier is." (ou)e started your declaration.

    $T%& +------oo at the symbols on the right of the identifier. If, say, you find "#"there, then you now that this is the declaration for a function. $o youwould then hae "identifier is function returning". r if you found a"!" there, you would say "identifier is array of". Continue right untilyou run out of symbols *R* hit a *right* parenthesis "#". If you hit aleft parenthesis, that)s the beginning of a # symbol, een if thereis stuff in between the parentheses. /ore on that below.#

    $T%& 0

    ------oo at the symbols to the left of the identifier. If it is not one of oursymbols aboe say, something lie "int"#, 1ust say it. therwise, translateit into %nglish using that table aboe. 2eep going left until you run out ofsymbols *R* hit a *left* parenthesis "".

    3ow repeat steps + and 0 until you)e formed your declaration. 4ere are somee5amples6

    int *p!7

    '# Find identifier. int *p!7 8 "p is"

    +# /oe right until out of symbols or left parenthesis hit. int *p!7

    88 "p is array of"

    0# Can)t moe right anymore out of symbols#, so moe left and find6 int *p!7 8 "p is array of pointer to"

    9# 2eep going left and find6 int *p!7 888 "p is array of pointer to int".

    Page 1 o# (

    http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.htmlhttp://ieng9.ucsd.edu/~cs30x/rt_lt.rule.htmlhttp://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html
  • 8/10/2019 Complex C Declarations

    2/3

    or "p is an array where each element is of type pointer to int"#

    :nother e5ample6

    int **func###7

    '# Find the identifier. int **func###7 8888

    "func is"

    +# /oe right. int **func###7 88 "func is function returning"

    0# Can)t moe right anymore because of the right parenthesis, so moe left. int **func###7 8 "func is function returning pointer to"

    9# Can)t moe left anymore because of the left parenthesis, so eep going right. int **func###7 88 "func is function returning pointer to function returning"

    ;# Can)t moe right anymore because we)re out of symbols, so go left.

    int **func###7 8 "func is function returning pointer to function returning pointer to"

    es and argument lists in prototype form. If you see "0!", that)s

    read as "array si>e 0# of...". If you see "char *,int#" that)s readas "function e5pecting char *,int# and returning...". 4ere)s a funone6

    int **fun?one#char *,double##@!+A!7

    I won)t go through each of the steps to decipher this one.

    . It)s6

    "fun?one is pointer to function e5pecting char *,double# andreturning pointer to array si>e @# of array si>e +A# of int."

    :s you can see, it)s not as complicated if you get rid of the array si>esand argument lists6

    int **fun?one###!!7

    (ou can decipher it that way, and then put in the array si>es and argumentlists later.

    $ome final words6

    It is =uite possible to mae illegal declarations using this rule,so some nowledge of what)s legal in C is necessary. For instance,if the aboe had been6

    int **fun?one###!!7

    Page 2 o# (

  • 8/10/2019 Complex C Declarations

    3/3

    it would hae been "fun?one is pointer to function returning array of array of 888888888888888888888888

    pointer to int". $ince a function cannot return an array, but only apointer to an array, that declaration is illegal.

    Illegal combinations include6

    !# - cannot hae an array of functions## - cannot hae a function that returns a function#! - cannot hae a function that returns an array

    In all the aboe cases, you would need a set of parens to bind a *symbol on the left between these # and ! right-side symbols in orderfor the declaration to be legal.

    4ere are some legal and illegal e5amples6

    int i7 an intint *p7 an int pointer ptr to an int#int a!7 an array of intsint f#7 a function returning an intint **pp7 a pointer to an int pointer ptr to a ptr to an int#int *pa#!7 a pointer to an array of intsint *pf##7 a pointer to a function returning an int

    int *ap!7 an array of int pointers array of ptrs to ints#int aa!!7 an array of arrays of intsint af!#7 an array of functions returning an int I%B:#

    int *fp#7 a function returning an int pointerint fa#!7 a function returning an array of ints I%B:#int ff##7 a function returning a function returning an int I%B:#int ***ppp7 a pointer to a pointer to an int pointerint **ppa#!7 a pointer to a pointer to an array of intsint **ppf##7 a pointer to a pointer to a function returning an intint **pap#!7 a pointer to an array of int pointersint *paa#!!7 a pointer to an array of arrays of intsint *paf#!#7 a pointer to a an array of functions returning an int I%B:#int **pfp##7 a pointer to a function returning an int pointerint *pfa##!7 a pointer to a function returning an array of ints I%B:#

    int *pff###7 a pointer to a function returning a function returning an int I%B:#int **app!7 an array of pointers to int pointersint *apa!#!7 an array of pointers to arrays of intsint *apf!##7 an array of pointers to functions returning an intint *aap!!7 an array of arrays of int pointersint aaa!!!7 an array of arrays of arrays of intsint aaf!!#7 an array of arrays of functions returning an int I%B:#int *afp!#7 an array of functions returning int pointers I%B:#int afa!#!7 an array of functions returning an array of ints I%B:#int aff!##7 an array of functions returning functions returning an int I%B:#int **fpp#7 a function returning a pointer to an int pointerint *fpa##!7 a function returning a pointer to an array of intsint *fpf###7 a function returning a pointer to a function

    returning an intint *fap#!7 a function returning an array of int pointers I%B:#int faa#!!7 a function returning an array of arrays of ints I%B:#int faf#!#7 a function returning an array of functions returning an int I%B:#int *ffp##7 a function returning a function returning an int pointer I%B:#

    Page ( o# (