Understanding a function declaration.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sri Harsha Dandibhotla

    Understanding a function declaration.

    Hello all.
    I recently came across a function declaration as :
    char(*(*x())[ ])();
    This was not in some code but it was in a C questions thread on some
    group.
    I tried to decipher what it returns but couldn't make complete sense
    out of it. Can someone please explain what this function is supposed
    to return and expand it step by step.

    Thanks,
    Harsha
  • Chris McDonald

    #2
    Re: Understanding a function declaration.

    Sri Harsha Dandibhotla <harsha.dsh@gma il.comwrites:
    >Hello all.
    >I recently came across a function declaration as :
    >char(*(*x())[ ])();
    >This was not in some code but it was in a C questions thread on some
    >group.
    >I tried to decipher what it returns but couldn't make complete sense
    >out of it. Can someone please explain what this function is supposed
    >to return and expand it step by step.

    Hello Harsha,

    You may like to seek out a helpful tool named 'cdecl',
    which is available as C source and, thus, runs on a variety of platforms.

    For your example it gives the answer:

    SHELL$ cdecl
    Type `help' or `?' for help
    cdecl explain char(*(*x())[ ])()
    declare x as function returning pointer to array of pointer to function returning char


    --
    Chris.

    Comment

    • Richard Heathfield

      #3
      Re: Understanding a function declaration.

      Sri Harsha Dandibhotla said:
      Hello all.
      I recently came across a function declaration as :
      char(*(*x())[ ])();
      This was not in some code but it was in a C questions thread on some
      group.
      I tried to decipher what it returns but couldn't make complete sense
      out of it. Can someone please explain what this function is supposed
      to return and expand it step by step.
      Start with x, the identifier.

      Look immediately right. Is it a [ ? No.

      Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.

      char(*(* )[ ])()
      x()
      "x is a function returning"

      Now read left. Is it a ( ? No.

      Is it a *, const, or volatile? If so, keep reading left till it isn't:

      char(*( )[ ])()
      *x()

      "x is a function returning a pointer to"

      Is the next thing on the left a (? Yes, and this is a grouping thing, so we
      can just balance out parentheses, without adding anything:

      char(* [ ])()
      (*x())

      "x is a function returning a pointer to"

      Look right. Is the thing on the right a [? Yes, it is.

      char(* )()
      (*x())[ ]

      "x is a function returning a pointer to an array of"

      Is the thing to the right a (? No.

      Is the thing to the left a (? No.

      Is the thing to the left a const, volatile, or *? Yes. Keep reading left until
      it isn't.

      char( )()
      *(*x())[ ]

      "x is a function returning a pointer to an array of pointer to"

      Is the thing to the left a (? Yes. Read up to the ).

      char ()
      (*(*x())[ ])

      "x is a function returning a pointer to an array of pointer to"

      Is the thing to the right a [ ? No.
      Is the thing to the right a (? Yes. Read up to the matching )...

      char
      (*(*x())[ ])()
      "x is a function returning a pointer to an array of pointer to function
      returning"

      Is the thing to the left a (? No.
      Is it const/volatile/*? No.
      Whatever's left, tack on the end:

      char (*(*x())[ ])()
      "x is a function returning a pointer to an array of pointer to function
      returning char"

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • Sri Harsha Dandibhotla

        #4
        Re: Understanding a function declaration.

        On Jun 16, 11:36 am, Chris McDonald <ch...@csse.uwa .edu.auwrote:
        Sri Harsha Dandibhotla <harsha....@gma il.comwrites:
        >
        Hello all.
        I recently came across a function declaration as :
        char(*(*x())[ ])();
        This was not in some code but it was in a C questions thread on some
        group.
        I tried to decipher what it returns but couldn't make complete sense
        out of it. Can someone please explain what this function is supposed
        to return and expand it step by step.
        >
        Hello Harsha,
        >
        You may like to seek out a helpful tool named 'cdecl',
        which is available as C source and, thus, runs on a variety of platforms.
        >
        For your example it gives the answer:
        >
        SHELL$ cdecl
        Type `help' or `?' for help
        cdecl explain char(*(*x())[ ])()
        declare x as function returning pointer to array of pointer to function returning char
        >
        Thank you.
        But, the tool gives the final translation, doesn't it.
        Is there any other tool or is there a command line option by which it
        outputs the translation as it parses?
        Example: Mr.Heathfield's translation(alt hough not as detailed.)

        Comment

        • vippstar@gmail.com

          #5
          Re: Understanding a function declaration.

          On Jun 16, 10:04 am, Sri Harsha Dandibhotla <harsha....@gma il.com>
          wrote:
          On Jun 16, 11:36 am, Chris McDonald <ch...@csse.uwa .edu.auwrote:
          >
          >
          >
          Sri Harsha Dandibhotla <harsha....@gma il.comwrites:
          >
          >Hello all.
          >I recently came across a function declaration as :
          >char(*(*x())[ ])();
          >This was not in some code but it was in a C questions thread on some
          >group.
          >I tried to decipher what it returns but couldn't make complete sense
          >out of it. Can someone please explain what this function is supposed
          >to return and expand it step by step.
          >
          Hello Harsha,
          >
          You may like to seek out a helpful tool named 'cdecl',
          which is available as C source and, thus, runs on a variety of platforms.
          >
          For your example it gives the answer:
          >
          SHELL$ cdecl
          Type `help' or `?' for help
          cdecl explain char(*(*x())[ ])()
          declare x as function returning pointer to array of pointer to function returning char
          >
          Thank you.
          But, the tool gives the final translation, doesn't it.
          Is there any other tool or is there a command line option by which it
          outputs the translation as it parses?
          Example: Mr.Heathfield's translation(alt hough not as detailed.)
          I think it's best to avoid such tools and instead learning to read the
          types as Mr Heathfield has demonstrated.

          Comment

          • Old Wolf

            #6
            Re: Understanding a function declaration.

            On Jun 16, 6:24 pm, Sri Harsha Dandibhotla <harsha....@gma il.com>
            wrote:
            Hello all.
            I recently came across a function declaration as :
            char(*(*x())[ ])();
            (*(*x())[]) is a function returning char

            therefore
            (*x())[] is a pointer to function returning char

            therefore
            *x() is an array of pointers to function returning char

            therefore
            x() is a pointer to an array of pointers to function returning char

            therefore
            x is a function returning a pointer to an array of
            pointers to function returning char

            Comment

            • Sri Harsha Dandibhotla

              #7
              Re: Understanding a function declaration.

              On Jun 16, 11:52 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
              Sri Harsha Dandibhotla said:
              >
              Hello all.
              I recently came across a function declaration as :
              char(*(*x())[ ])();
              This was not in some code but it was in a C questions thread on some
              group.
              I tried to decipher what it returns but couldn't make complete sense
              out of it. Can someone please explain what this function is supposed
              to return and expand it step by step.
              >
              Start with x, the identifier.
              >
              Look immediately right. Is it a [ ? No.
              >
              Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.
              >
              char(*(* )[ ])()
              x()
              "x is a function returning"
              >
              <rest snipped>

              This is where I have trouble understanding. Do the parentheses on the
              outside accept arguments are do those on the inside beside x.
              that is, if I have to define the function, will it be

              char(*(*x())[ ])(/*arguments to function*/)
              {
              /*function body*/
              }

              or will it be
              char(*(*x(/*arguments to function*/))[ ])()
              {
              /*function body*/
              }

              Comment

              • Richard Heathfield

                #8
                Re: Understanding a function declaration.

                Sri Harsha Dandibhotla said:
                On Jun 16, 11:52 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                >Sri Harsha Dandibhotla said:
                >>
                Hello all.
                I recently came across a function declaration as :
                char(*(*x())[ ])();
                This was not in some code but it was in a C questions thread on some
                group.
                I tried to decipher what it returns but couldn't make complete sense
                out of it. Can someone please explain what this function is supposed
                to return and expand it step by step.
                >>
                >Start with x, the identifier.
                >>
                >Look immediately right. Is it a [ ? No.
                >>
                >Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.
                >>
                >char(*(* )[ ])()
                > x()
                >"x is a function returning"
                >>
                <rest snipped>
                >
                This is where I have trouble understanding. Do the parentheses on the
                outside accept arguments are do those on the inside beside x.
                Either can, and neither must.
                that is, if I have to define the function, will it be
                >
                char(*(*x())[ ])(/*arguments to function*/)
                {
                /*function body*/
                }
                >
                or will it be
                char(*(*x(/*arguments to function*/))[ ])()
                {
                /*function body*/
                }
                typedef is your friend.

                We have a function returning a pointer to an array of pointer to function
                returning char. Let's start off by defining a type, "function returning char".
                I invent the convention that ft_ means "function type", and it is followed by
                a printf-modifier-style abbrev representing the return type and, optionally,
                something along the same lines for parameters:

                typedef char ft_c(); /* ft_c is a synonym for the type "function returning
                char" */

                Now our declaration is reduced to:

                ft_c *(*x())[];

                which we might read as: "x is a function returning a pointer to an array of
                pointer to ft_c", and the answer to your question now becomes clear. If we
                wish for x() to take parameters, they go in the innermost parentheses, the
                ones right after the identifier:

                ft_c *(*x(double, char *))[];

                "x is a function taking a double and a char *, which returns a pointer to an
                array of pointer to ft_c"

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • Richard

                  #9
                  Re: Understanding a function declaration.

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  Sri Harsha Dandibhotla said:
                  >
                  >Hello all.
                  >I recently came across a function declaration as :
                  >char(*(*x())[ ])();
                  >This was not in some code but it was in a C questions thread on some
                  >group.
                  >I tried to decipher what it returns but couldn't make complete sense
                  >out of it. Can someone please explain what this function is supposed
                  >to return and expand it step by step.
                  >
                  Start with x, the identifier.
                  >
                  Look immediately right. Is it a [ ? No.
                  >
                  Is it a (? Yes: x( Keep reading until you hit the closing parenthesis.
                  >
                  char(*(* )[ ])()
                  x()
                  "x is a function returning"
                  >
                  Now read left. Is it a ( ? No.
                  >
                  Is it a *, const, or volatile? If so, keep reading left till it isn't:
                  >
                  char(*( )[ ])()
                  *x()
                  >
                  "x is a function returning a pointer to"
                  >
                  Is the next thing on the left a (? Yes, and this is a grouping thing, so we
                  can just balance out parentheses, without adding anything:
                  >
                  char(* [ ])()
                  (*x())
                  >
                  "x is a function returning a pointer to"
                  >
                  Look right. Is the thing on the right a [? Yes, it is.
                  >
                  char(* )()
                  (*x())[ ]
                  >
                  "x is a function returning a pointer to an array of"
                  >
                  Is the thing to the right a (? No.
                  >
                  Is the thing to the left a (? No.
                  >
                  Is the thing to the left a const, volatile, or *? Yes. Keep reading left until
                  it isn't.
                  >
                  char( )()
                  *(*x())[ ]
                  >
                  "x is a function returning a pointer to an array of pointer to"
                  >
                  Is the thing to the left a (? Yes. Read up to the ).
                  >
                  char ()
                  (*(*x())[ ])
                  >
                  "x is a function returning a pointer to an array of pointer to"
                  >
                  Is the thing to the right a [ ? No.
                  Is the thing to the right a (? Yes. Read up to the matching )...
                  >
                  char
                  (*(*x())[ ])()
                  "x is a function returning a pointer to an array of pointer to function
                  returning"
                  >
                  Is the thing to the left a (? No.
                  Is it const/volatile/*? No.
                  Whatever's left, tack on the end:
                  >
                  char (*(*x())[ ])()
                  "x is a function returning a pointer to an array of pointer to function
                  returning char"
                  I must say, well explained - but highlights why I would never ask a
                  question like this in an interview. It's nigh on impossible to do it in
                  the head. Well, in my booze addled one anyway.

                  Comment

                  • Bart

                    #10
                    Re: Understanding a function declaration.


                    "Sri Harsha Dandibhotla" <harsha.dsh@gma il.comwrote in message
                    news:bc561c31-
                    c29e-42ed-9e92-73e6e2556057@l6 4g20...l egroups.com...
                    Hello all.
                    I recently came across a function declaration as :
                    char(*(*x())[ ])();
                    This was not in some code but it was in a C questions thread on some
                    group.
                    I tried to decipher what it returns but couldn't make complete sense
                    out of it. Can someone please explain what this function is supposed
                    to return and expand it step by step.
                    I once proposed here a linear, left-to-right notation for C
                    declarations:

                    Your example would have come out as (using 'function' for extra
                    clarity):

                    function() * [] * function() char x

                    Read as "function returning pointer to array of pointer to function
                    returning char, x"

                    No complicated algorithm, you just need to be able to read.

                    But I don't remember there was much approval. Perhaps once somebody
                    does master the declarations they don't want their efforts undermined!
                    Aside from it being rather difficult to incorporate into C at this
                    stage.

                    Fortunately examples such as yours aren't common.

                    --
                    Bartc

                    Comment

                    • shantanu

                      #11
                      Re: Understanding a function declaration.

                      On Jun 16, 11:24 am, Sri Harsha Dandibhotla <harsha....@gma il.com>
                      wrote:
                      Hello all.
                      I recently came across a function declaration as :
                      char(*(*x())[ ])();
                      This was not in some code but it was in a C questions thread on some
                      group.
                      I tried to decipher what it returns but couldn't make complete sense
                      out of it. Can someone please explain what this function is supposed
                      to return and expand it step by step.
                      >
                      Thanks,
                      Harsha
                      Hi Harsha,

                      if start with x() and move outwards it makes the things very simple.
                      the above mentioned statement reads x is a function which return a
                      pointer to an array of pointer to function which returns a character.

                      Shantanu.
                      Shantanu

                      Comment

                      • John Bode

                        #12
                        Re: Understanding a function declaration.

                        On Jun 16, 1:24 am, Sri Harsha Dandibhotla <harsha....@gma il.com>
                        wrote:
                        Hello all.
                        I recently came across a function declaration as :
                        char(*(*x())[ ])();
                        This was not in some code but it was in a C questions thread on some
                        group.
                        I tried to decipher what it returns but couldn't make complete sense
                        out of it. Can someone please explain what this function is supposed
                        to return and expand it step by step.
                        >
                        Thanks,
                        Harsha
                        Find the leftmost identifier, then work your way out, remembering that
                        [] and () bind before *:

                        x -- x
                        x() -- is a function
                        *x() -- returning a pointer
                        (*x())[] -- to an array
                        *(*x())[] -- of pointers
                        (*(*x())[])() -- to functions
                        char (*(*x())[])() -- returning char

                        Comment

                        • Sri Harsha Dandibhotla

                          #13
                          Re: Understanding a function declaration.

                          On Jun 16, 9:31 pm, John Bode <john_b...@my-deja.comwrote:
                          On Jun 16, 1:24 am, Sri Harsha Dandibhotla <harsha....@gma il.com>
                          wrote:
                          >
                          Hello all.
                          I recently came across a function declaration as :
                          char(*(*x())[ ])();
                          This was not in some code but it was in a C questions thread on some
                          group.
                          I tried to decipher what it returns but couldn't make complete sense
                          out of it. Can someone please explain what this function is supposed
                          to return and expand it step by step.
                          >
                          Thanks,
                          Harsha
                          >
                          Find the leftmost identifier, then work your way out, remembering that
                          [] and () bind before *:
                          >
                          x -- x
                          x() -- is a function
                          *x() -- returning a pointer
                          (*x())[] -- to an array
                          *(*x())[] -- of pointers
                          (*(*x())[])() -- to functions
                          char (*(*x())[])() -- returning ch
                          I thank you all for the responses. My only confusion was with whether
                          the inner parentheses determine the arguments to be passed or the
                          outer ones.
                          It has been clarified.

                          Comment

                          • CBFalconer

                            #14
                            Re: Understanding a function declaration.

                            Sri Harsha Dandibhotla wrote:
                            >
                            I recently came across a function declaration as :
                            char(*(*x())[ ])();
                            This was not in some code but it was in a C questions thread on
                            some group. I tried to decipher what it returns but couldn't
                            make complete sense out of it. Can someone please explain what
                            this function is supposed to return and expand it step by step.
                            [1] c:\djgpp\includ e>cdecl
                            Type `help' or `?' for help
                            cdeclexplain char(*(*x())[ ])()
                            declare x as function returning pointer to array of pointer to
                            function returning char

                            (last is one line)

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            ** Posted from http://www.teranews.com **

                            Comment

                            Working...