Analyzing and Creating Complex Declarations

Chapter 8:Preprocessor And Complex Declarations

    Analyzing and Creating Complex Declarations

  •           Most of the occasion declarations are easy to read, but it is tough to read and to understand some declarations which involve pointer to functions. To decipher C declarations the "right-left" rule is generally used. It is also useful in creating them.

    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 meet them in the declaration.

    Steps to read complex declarations:

                   1. Change the declaration to postfix arrangement and read from left to right.

                   2. To change expression to postfix, begin from innermost parenthesis, if innermost parenthesis is not there then start from declarations name and go right first. When first closing parenthesis occurs then go left. Once entire parenthesis is parsed then exit from parenthesis.

                   3. Don’t stop this process until entire declaration has been parsed.

    Example:

    1)  int (*funptr) ();

              First  convert above expression to postfix format.

              funptr  *  ()  int

              In this example, there is no innermost parenthesis, so write declaration name i.e. “funptr”. Then, go to right side of expression, but there is nothing on right side of “funptr” to parse, so go to left side. On left side “*” is found, now write [*] and exit from parenthesis. Now read postfix expression from left to right. e.g. funptr is pointer to function returning int.

    2) int *p[15];

              Postfix format: p * [15] int
              Meaning of the expression: p is pointer to array of 13 integers.

    3) void (*arg[20]) (int, int)

              Postfix format : arg[20] * (int, int) void
             

              Meaning of the expression: arg is an array of 20 of pointer to function(that accepts 2

              arguments of type int) returning void.

    4) int *(*f ())();

              Postfix format:    f () * () * int
              Meaning of the expression: f is function returning pointer to function returning pointer to int.

    5) int **disp();

              Postfix format: disp () *  int *
              Meaning of the expression: Disp is a function returning a pointer to an int pointer.

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy