The right-left rule

Source: Internet
Author: User

The right-left rule:

Start reading the declaration from the innermost parentheses, go right, and then go left. when you encounter parentheses, the direction shoshould be reversed. once everything in the parentheses has been parsed, jump out of it. continue till the whole
Declaration has been parsed.

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 sideas you encounter them in the declaration.STEP 1------Find the identifier.  This is your starting point.  Then say to yourself,"identifier is."  You've started your declaration.STEP 2------Look at the symbols on the right of the identifier.  If, say, you find "()"there, then you know that this is the declaration for a function.  So youwould then have "identifier is function returning".  Or if you found a "[]" there, you would say "identifier is array of".  Continue right untilyou run out of symbols *OR* hit a *right* parenthesis ")".  (If you hit a left parenthesis, that's the beginning of a () symbol, even if thereis stuff in between the parentheses.  More on that below.)STEP 3------Look at the symbols to the left of the identifier.  If it is not one of oursymbols above (say, something like "int"), just say it.  Otherwise, translateit into English using that table above.  Keep going left until you run out ofsymbols *OR* hit a *left* parenthesis "(".  Now repeat steps 2 and 3 until you've formed your declaration.  Here are someexamples:     int *p[];1) Find identifier.          int *p[];                                  ^   "p is"2) Move right until out of symbols or left parenthesis hit.                             int *p[];                                   ^^   "p is array of"3) Can't move right anymore (out of symbols), so move left and find:                             int *p[];                                 ^   "p is array of pointer to"4) Keep going left and find:                             int *p[];                             ^^^   "p is array of pointer to int".    (or "p is an array where each element is of type pointer to int")Another example:   int *(*func())();1) Find the identifier.      int *(*func())();                                    ^^^^   "func is"2) Move right.               int *(*func())();                                        ^^   "func is function returning"3) Can't move right anymore because of the right parenthesis, so move left.                             int *(*func())();                                   ^   "func is function returning pointer to"4) Can't move left anymore because of the left parenthesis, so keep going   right.                    int *(*func())();                                           ^^   "func is function returning pointer to function returning"5) Can't move right anymore because we're out of symbols, so go left.                             int *(*func())();                                 ^   "func is function returning pointer to function returning pointer to"6) And finally, keep going left, because there's nothing left on the right.                             int *(*func())();                             ^^^   "func is function returning pointer to function returning pointer to int".As you can see, this rule can be quite useful.  You can also use it tosanity check yourself while you are creating declarations, and to giveyou a hint about where to put the next symbol and whether parenthesesare required.Some declarations look much more complicated than they are due to arraysizes and argument lists in prototype form.  If you see "[3]", that'sread as "array (size 3) of...".  If you see "(char *,int)" that's readas "function expecting (char *,int) and returning...".  Here's a funone:                 int (*(*fun_one)(char *,double))[9][20];I won't go through each of the steps to decipher this one.Ok.  It's:     "fun_one is pointer to function expecting (char *,double) and       returning pointer to array (size 9) of array (size 20) of int."As you can see, it's not as complicated if you get rid of the array sizesand argument lists:     int (*(*fun_one)())[][];You can decipher it that way, and then put in the array sizes and argumentlists later.Some final words:It is quite possible to make illegal declarations using this rule,so some knowledge of what's legal in C is necessary.  For instance,if the above had been:     int *((*fun_one)())[][];it would have been "fun_one is pointer to function returning array of array of                                          ^^^^^^^^^^^^^^^^^^^^^^^^pointer to int".  Since a function cannot return an array, but only a pointer to an array, that declaration is illegal.Illegal combinations include: []() - cannot have an array of functions ()() - cannot have a function that returns a function ()[] - cannot have a function that returns an arrayIn all the above 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.Here are some legal and illegal examples:int i;                  an intint *p;                 an int pointer (ptr to an int)int a[];                an array of intsint f();                a function returning an intint **pp;               a pointer to an int pointer (ptr to a ptr to an int)int (*pa)[];            a pointer to an array of intsint (*pf)();            a pointer to a function returning an intint *ap[];              an array of int pointers (array of ptrs to ints)int aa[][];             an array of arrays of intsint af[]();             an array of functions returning an int (ILLEGAL)int *fp();              a function returning an int pointerint fa()[];             a function returning an array of ints (ILLEGAL)int ff()();             a function returning a function returning an int                                (ILLEGAL)int ***ppp;             a pointer to a pointer to an int pointerint (**ppa)[];          a pointer to a pointer to an array of intsint (**ppf)();          a pointer to a pointer to a function returning an intint *(*pap)[];          a pointer to an array of int pointersint (*paa)[][];         a pointer to an array of arrays of intsint (*paf)[]();         a pointer to a an array of functions returning an int                                (ILLEGAL)int *(*pfp)();          a pointer to a function returning an int pointerint (*pfa)()[];         a pointer to a function returning an array of ints                                (ILLEGAL)int (*pff)()();         a pointer to a function returning a function                                returning an int (ILLEGAL)int **app[];            an array of pointers to int pointersint (*apa[])[];         an array of pointers to arrays of intsint (*apf[])();         an array of pointers to functions returning an intint *aap[][];           an array of arrays of int pointersint aaa[][][];          an array of arrays of arrays of intsint aaf[][]();          an array of arrays of functions returning an int                                (ILLEGAL)int *afp[]();           an array of functions returning int pointers (ILLEGAL)int afa[]()[];          an array of functions returning an array of ints                                (ILLEGAL)int aff[]()();          an array of functions returning functions                                returning an int (ILLEGAL)int **fpp();            a function returning a pointer to an int pointerint (*fpa())[];         a function returning a pointer to an array of intsint (*fpf())();         a function returning a pointer to a function                                returning an intint *fap()[];           a function returning an array of int pointers (ILLEGAL)int faa()[][];          a function returning an array of arrays of ints                                (ILLEGAL)int faf()[]();          a function returning an array of functions                                returning an int (ILLEGAL)int *ffp()();           a function returning a function                                returning an int pointer (ILLEGAL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.