Difference between function pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ram kishore

    Difference between function pointers

    What is the difference between
    (int *) fun() and int *fun() ?

    What is the advantage of pointers to functions?
  • jacob navia

    #2
    Re: Difference between function pointers

    ram kishore wrote:
    What is the difference between
    (int *) fun() and int *fun() ?
    >
    What is the advantage of pointers to functions?
    Do your own homework

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique

    Comment

    • Richard Heathfield

      #3
      Re: Difference between function pointers

      ram kishore said:
      What is the difference between
      (int *) fun() and int *fun() ?
      ()
      What is the advantage of pointers to functions?
      When you need to point to a function, nothing beats them.

      --
      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

      • zR0

        #4
        Re: Difference between function pointers

        On Jul 25, 12:19 pm, ram kishore <rapol...@gmail .comwrote:
        What  is the difference between
        (int *) fun() and int *fun() ?
        >
        What is the advantage of pointers to functions?
        This can be a good resource on this topic.

        Comment

        • Richard Bos

          #5
          Re: Difference between function pointers

          zR0 <rohit.n.sarpot dar@gmail.comwr ote:
          On Jul 25, 12:19=A0pm, ram kishore <rapol...@gmail .comwrote:
          What =A0is the difference between
          (int *) fun() and int *fun() ?

          What is the advantage of pointers to functions?
          >
          This can be a good resource on this topic.
          http://www.newty.de/fpt/index.html
          Not if you're programming in C, it can't. That's very C++-oriented.

          Richard

          Comment

          • Nick Keighley

            #6
            Re: Difference between function pointers

            On 25 Jul, 08:19, ram kishore <rapol...@gmail .comwrote:
            What  is the difference between
            (int *) fun() and int *fun() ?
            one gives a syntax error the other doesn't
            What is the advantage of pointers to functions?
            1. call backs
            2. threaded interpreters
            3. really cool looking code

            look up qsort() in your textbook

            --
            Nick Keighley

            Error on line 0: Lazy programmer.

            Comment

            • Keith Thompson

              #7
              Re: Difference between function pointers

              Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
              On 25 Jul, 08:19, ram kishore <rapol...@gmail .comwrote:
              >
              >What  is the difference between
              >(int *) fun() and int *fun() ?
              >
              one gives a syntax error the other doesn't
              [...]

              Not necessarily. I just wrote a small syntactically correct program
              that uses both constructs. But one of them probably doesn't mean what
              the OP thinks it means.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • pete

                #8
                Re: Difference between function pointers

                ram kishore wrote:
                What is the advantage of pointers to functions?
                All function calls are made
                with an operand of pointer to function type.

                --
                pete

                Comment

                • Keith Thompson

                  #9
                  Re: Difference between function pointers

                  pete <pfiland@mindsp ring.comwrites:
                  ram kishore wrote:
                  >What is the advantage of pointers to functions?
                  >
                  All function calls are made
                  with an operand of pointer to function type.
                  True.

                  Then the question the OP should have asked is:

                  What is the advantage of pointers to functions other than those that
                  result from the implicit conversion of a function name used as the
                  prefix of a function call?

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • pete

                    #10
                    Re: Difference between function pointers

                    Keith Thompson wrote:
                    pete <pfiland@mindsp ring.comwrites:
                    >ram kishore wrote:
                    >>What is the advantage of pointers to functions?
                    >All function calls are made
                    >with an operand of pointer to function type.
                    >
                    True.
                    >
                    Then the question the OP should have asked is:
                    >
                    What is the advantage of pointers to functions other than those that
                    result from the implicit conversion of a function name used as the
                    prefix of a function call?
                    I recall recently,
                    in one of the web pages from a URL posted to this newsgroup,
                    advice that function calls should properly be made this way:
                    either
                    function_name() ;
                    or
                    (*function_poin ter)();

                    I wouldn't write function calls differently
                    for function name versus pointer constructs,
                    but if I would, I would do it this way:
                    either
                    (&function_name )();
                    or
                    function_pointe r();

                    The other way doesn't make any sense.

                    --
                    pete

                    Comment

                    • Greg Comeau

                      #11
                      Re: Difference between function pointers

                      In article <Drednbtjw4FtPR bVnZ2dnUVZ_uWdn Z2d@earthlink.c om>,
                      pete <pfiland@mindsp ring.comwrote:
                      >Keith Thompson wrote:
                      >pete <pfiland@mindsp ring.comwrites:
                      >>ram kishore wrote:
                      >>>What is the advantage of pointers to functions?
                      >>All function calls are made
                      >>with an operand of pointer to function type.
                      >>
                      >True.
                      >>
                      >Then the question the OP should have asked is:
                      >>
                      >What is the advantage of pointers to functions other than those that
                      >result from the implicit conversion of a function name used as the
                      >prefix of a function call?
                      >
                      >I recall recently,
                      >in one of the web pages from a URL posted to this newsgroup,
                      >advice that function calls should properly be made this way:
                      >either
                      function_name() ;
                      >or
                      (*function_poin ter)();
                      >
                      >I wouldn't write function calls differently
                      >for function name versus pointer constructs,
                      >but if I would, I would do it this way:
                      >either
                      (&function_name )();
                      >or
                      function_pointe r();
                      >
                      >The other way doesn't make any sense.
                      I may be misunderstandin g you, and/or the other P above, but seems
                      to me that although foo() vs (*foo)() etc is a valid discussion,
                      that it was not the original context of the question, which
                      was delving into why have functions which can be pointed to at all,
                      and not delving into stuff like their sytactic equivalences.
                      That said, as to your point, I probably agree with you and it's
                      kind of just another set of confusion for most people how this
                      can all be so:

                      #include <stdio.h>

                      void foo()
                      {
                      printf("foo\n") ;
                      }

                      void (*pf)();

                      int main()
                      {
                      foo();
                      (*foo)();
                      (&foo)();

                      pf = foo;
                      pf();
                      (*pf)();
                      (&pf)(); // error
                      }
                      --
                      Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
                      Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
                      World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                      Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: Difference between function pointers

                        comeau@panix.co m (Greg Comeau) writes:
                        In article <lnfxpvpuug.fsf @nuthaus.mib.or g>,
                        Keith Thompson <kst-u@mib.orgwrote:
                        <snip>
                        >>A call using just the name of a declared
                        >>function is the common case; perhaps 99% of calls are of that form.
                        >>It makes sense to use the shortest form for the common case. The point
                        >>of writing
                        >>
                        > function_name() ;
                        > (*function_poin ter)();
                        >>
                        >>is of course to emphasize the fact that the latter is using a pointer
                        >>object, just as we distinguish between struct_name.mem ber and
                        >>pointer_nam e->member.
                        >
                        Yup. And there are some historical "distortion s" tossed in too
                        (for instance, if I recall, function_name() when function_name
                        was a pointer (sic) was not accepted by many early C compilers).
                        Absolutely. Both the old C reference manual I have and my copy of K&R
                        (1st edition) are quite clear that you call functions, not pointers.
                        In the expression form

                        primary-expression ( expression-list /opt/ )

                        the primary expression is required to have type "function returning
                        ....". Later both say "If the name of a function appears in an
                        expression not in the function-name position of a call, a pointer to
                        the function is generated.

                        It took me a while to get used to the new-fangled idea of *not*
                        writing (*f)(...).

                        --
                        Ben.

                        Comment

                        Working...