How do I create a function in my library for passing user callback function

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

    How do I create a function in my library for passing user callback function

    Hello

    I am writing a library which will write data to a user defined callback
    function. The function the user of my library will supply is:

    int (*callbackfunct ion)(const char*);

    In my libary do I create a function where user passes this callback
    function? How would I define the function?

    I tried this

    callbackfunctio n clientfunction;

    void SpecifyCallback function(cbFunc tion cbFn)
    {
    clientfunction = cbFn;
    }

    Then called like this:
    clientfunction( sz); // sz is a C-string.

    But program crashes with access violation when attempt to call
    clientfunction



    What am I doing wrong?


  • Ian Collins

    #2
    Re: How do I create a function in my library for passing user callbackfunctio n

    Angus wrote:
    Hello
    >
    I am writing a library which will write data to a user defined callback
    function. The function the user of my library will supply is:
    >
    int (*callbackfunct ion)(const char*);
    >
    In my libary do I create a function where user passes this callback
    function? How would I define the function?
    >
    I tried this
    >
    callbackfunctio n clientfunction;
    >
    void SpecifyCallback function(cbFunc tion cbFn)
    {
    clientfunction = cbFn;
    }
    >
    Then called like this:
    clientfunction( sz); // sz is a C-string.
    >
    You should be passing the address of the function, not a string.

    int f( const char* );

    clientfunction( f );

    --
    Ian Collins.

    Comment

    • Richard Heathfield

      #3
      Re: How do I create a function in my library for passing user callback function

      Ian Collins said:
      Angus wrote:
      >Hello
      >>
      >I am writing a library which will write data to a user defined callback
      >function. The function the user of my library will supply is:
      >>
      >int (*callbackfunct ion)(const char*);
      >>
      >In my libary do I create a function where user passes this callback
      >function? How would I define the function?
      >>
      >I tried this
      >>
      >callbackfuncti on clientfunction;
      >>
      >void SpecifyCallback function(cbFunc tion cbFn)
      >{
      > clientfunction = cbFn;
      >}
      >>
      >Then called like this:
      >clientfunction (sz); // sz is a C-string.
      >>
      You should be passing the address of the function, not a string.
      >
      int f( const char* );
      >
      clientfunction( f );
      I doubt it. Since clientfunction is an instance of callbackfunctio n (and
      presumably the definition of callbackfunctio n, above, is supposed to be a
      typedef), it takes a const char *, not an int(*)(const 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

      • Ian Collins

        #4
        Re: How do I create a function in my library for passing user callbackfunctio n

        Richard Heathfield wrote:
        Ian Collins said:
        >
        >Angus wrote:
        >>Hello
        >>>
        >>I am writing a library which will write data to a user defined callback
        >>function. The function the user of my library will supply is:
        >>>
        >>int (*callbackfunct ion)(const char*);
        >>>
        >>In my libary do I create a function where user passes this callback
        >>function? How would I define the function?
        >>>
        >>I tried this
        >>>
        >>callbackfunct ion clientfunction;
        >>>
        >>void SpecifyCallback function(cbFunc tion cbFn)
        >>{
        >> clientfunction = cbFn;
        >>}
        >>>
        >>Then called like this:
        >>clientfunctio n(sz); // sz is a C-string.
        >>>
        >You should be passing the address of the function, not a string.
        >>
        >int f( const char* );
        >>
        >clientfunction ( f );
        >
        I doubt it. Since clientfunction is an instance of callbackfunctio n (and
        presumably the definition of callbackfunctio n, above, is supposed to be a
        typedef), it takes a const char *, not an int(*)(const char *).
        >
        OK, I promise never to post pre-caffeine ever again!

        I read the OP as passing a string to SpecifyCallback function.

        --
        Ian Collins.

        Comment

        • Ben Bacarisse

          #5
          Re: How do I create a function in my library for passing user callback function

          "Angus" <nospam@gmail.c omwrites:
          I am writing a library which will write data to a user defined callback
          function. The function the user of my library will supply is:
          >
          int (*callbackfunct ion)(const char*);
          >
          In my libary do I create a function where user passes this callback
          function? How would I define the function?
          >
          I tried this
          >
          callbackfunctio n clientfunction;
          This won't compile. I suspect you have a typedef that you are not
          showing us!

          <snip>
          But program crashes with access violation when attempt to call
          clientfunction
          >
          What am I doing wrong?
          Try to post a short, compilable, example of the problem. The outline
          you posted is sound, the error is in the detail (and is somewhere
          else).

          --
          Ben.

          Comment

          • Bill Reid

            #6
            Re: How do I create a function in my library for passing user callback function


            Angus <nospam@gmail.c omwrote in message
            news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
            Hello
            >
            I am writing a library which will write data to a user defined callback
            function. The function the user of my library will supply is:
            >
            int (*callbackfunct ion)(const char*);
            >
            In my libary do I create a function where user passes this callback
            function? How would I define the function?
            >
            I tried this
            >
            callbackfunctio n clientfunction;
            >
            void SpecifyCallback function(cbFunc tion cbFn)
            {
            clientfunction = cbFn;
            }
            >
            Then called like this:
            clientfunction( sz); // sz is a C-string.
            >
            But program crashes with access violation when attempt to call
            clientfunction
            >
            What am I doing wrong?
            Well, just about everything, and most pertinently, asking a question
            here, the land of the technically-incompetent trolls...but here's how
            you do it:

            In the header file for your library, declare the function as follows:

            extern void my_library_func tion(int (*)(const char*));

            (Note: as somebody may tell you, "extern" is a redundant
            linkage specifier for function declarations, but I use it anyway
            and therefore you should too!)

            Now write your library function that takes the callback as
            a parameter in the source file for your library:

            void my_library_func tion(int my_callback_fun ction(const char*)) {
            int my_callback_ret urn;
            char *my_string;

            ... /* generic stuff done here, probably "build up" my_string */

            my_callback_ret urn=my_callback _function(my_st ring);

            ... /* more generic stuff maybe, maybe check my_callback_ret urn */
            }

            Now, for any source file that you want to use that generic
            my_library_func tion(), you can call it by first #include'ing the
            library header file, then defining a specific callback function that
            matches the declaration in the header file:

            int my_specific_fun ction(const char* my_string) {

            ... /* do something with string, probably print it, right? */
            }

            Then you can call your library function with the callback anywhere
            in your source file, as well as any other functions that you have defined
            that match the callback signature:

            void my_function(voi d) {

            ... /* stuff happens here, whatever, maybe nothing, who knows */

            my_library_func tion(my_specifi c_function);

            ... /* and whatever else */
            }

            And that's "all" there is to it...not that bad once you get the hang of
            it, just follow the pattern above, sometimes you have to really "think"
            about what the perfect "signature" will be for all the various callbacks
            you want for a generic library function, what all data you need to
            pass for all possible conditions...

            ---
            William Ernest Reid



            Comment

            • Richard Heathfield

              #7
              Re: How do I create a function in my library for passing user callback function

              Bill Reid said:
              Angus <nospam@gmail.c omwrote in message
              news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
              >
              <snip>
              >>
              >What am I doing wrong?
              >
              Well, just about everything, and most pertinently, asking a question
              here, the land of the technically-incompetent trolls...but here's how
              you do it:
              When you make a claim like that, you should back it up with working code.
              You didn't. A conforming implementation *must* diagnose, and *may* refuse
              to translate, your code.

              Your track record of ignoring or even railing against those who notify you
              of your mistakes gives me little or no hope that you'll pay any attention
              to this, but the OP will at least have fair warning of the kind of
              "competence " to which you are exposing him.

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

              • Bill Reid

                #8
                Re: How do I create a function in my library for passing user callback function


                Richard Heathfield <rjh@see.sig.in validwrote in message
                news:a96dnbUf7a asdp_VRVnygwA@b t.com...
                Bill Reid said:
                >
                Angus <nospam@gmail.c omwrote in message
                news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
                <snip>
                >
                What am I doing wrong?
                Well, just about everything, and most pertinently, asking a question
                here, the land of the technically-incompetent trolls...but here's how
                you do it:
                >
                When you make a claim like that, you should back it up with working code.
                You didn't. A conforming implementation *must* diagnose, and *may* refuse
                to translate, your code.
                Well, I didn't really write any code, you insane troll, so I guess
                you're "right" again, as always...
                Your track record of ignoring or even railing against those who notify you
                of your mistakes gives me little or no hope that you'll pay any attention
                to this, but the OP will at least have fair warning of the kind of
                "competence " to which you are exposing him.
                Well, no, I couldn't help but notice that you didn't specify any
                actual problems with what I posted, so how would he know what's
                wrong with it? Believe me, for his sake, you should let HIM know,
                and let ALL of us know, including me, so we can all benefit from
                your tremendous "wisdom"... I'd be the first to admit that I make
                a LOT of mistakes, have made many here, and there may be
                errors or omissions in my post, and I'd genuinely like to be
                "set straight", but somehow I think you're just blowin' troll
                smoke again, as usual...

                So if you got anything of substance, post it; I'm not saying it
                would be a first, but along the lines of a rarity...

                ---
                William Ernest Reid



                Comment

                • Richard Heathfield

                  #9
                  Re: How do I create a function in my library for passing user callback function

                  Bill Reid said:
                  >
                  Richard Heathfield <rjh@see.sig.in validwrote in message
                  news:a96dnbUf7a asdp_VRVnygwA@b t.com...
                  >Bill Reid said:
                  >>
                  Angus <nospam@gmail.c omwrote in message
                  news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
                  >
                  ><snip>
                  >>
                  >What am I doing wrong?
                  >
                  Well, just about everything, and most pertinently, asking a question
                  here, the land of the technically-incompetent trolls...but here's how
                  you do it:
                  >>
                  >When you make a claim like that, you should back it up with working
                  >code. You didn't. A conforming implementation *must* diagnose, and *may*
                  >refuse to translate, your code.
                  >
                  Well, I didn't really write any code, you insane troll, so I guess
                  you're "right" again, as always...
                  Here is the code you claim you didn't really write, which I've copied
                  verbatim from your article.

                  extern void my_library_func tion(int (*)(const char*));
                  void my_library_func tion(int my_callback_fun ction(const char*)) {
                  int my_callback_ret urn;
                  char *my_string;

                  ... /* generic stuff done here, probably "build up" my_string */

                  my_callback_ret urn=my_callback _function(my_st ring);

                  ... /* more generic stuff maybe, maybe check my_callback_ret urn */
                  }
                  int my_specific_fun ction(const char* my_string) {

                  ... /* do something with string, probably print it, right? */
                  }
                  void my_function(voi d) {

                  ... /* stuff happens here, whatever, maybe nothing, who knows */

                  my_library_func tion(my_specifi c_function);

                  ... /* and whatever else */
                  }

                  Note that my observation about failure to compile does not relate to
                  obvious "more stuff goes here" conventions such as an occasional ellipsis.
                  >Your track record of ignoring or even railing against those who notify
                  >you of your mistakes gives me little or no hope that you'll pay any
                  >attention to this, but the OP will at least have fair warning of the
                  >kind of "competence " to which you are exposing him.
                  >
                  Well, no, I couldn't help but notice that you didn't specify any
                  actual problems with what I posted,
                  No point. You never listen anyway.
                  so how would he know what's wrong with it?
                  Since it doesn't compile, he'll find out pretty quickly that it *is* wrong.
                  As to *why* it's wrong, that's easy. It was written by someone who doesn't
                  understand C very well.
                  So if you got anything of substance, post it;
                  After you.

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

                  • Nick Keighley

                    #10
                    Re: How do I create a function in my library for passing usercallback function

                    On 14 Apr, 03:24, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                    Angus <nos...@gmail.c omwrote in message
                    news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
                    I am writing a library which will write data to a user defined callback
                    function.  The function the user of my library will supply is:
                    >
                    int (*callbackfunct ion)(const char*);
                    >
                    In my libary do I create a function where user passes this callback
                    function?  How would I define the function?
                    >
                    I tried this
                    >
                    callbackfunctio n clientfunction;
                    >
                    void SpecifyCallback function(cbFunc tion cbFn)
                    what is cbFunction?
                    {
                       clientfunction = cbFn;
                    }
                    >
                    Then called like this:
                    clientfunction( sz);  // sz is a C-string.
                    >
                    But program crashes with access violation when attempt to call
                    clientfunction
                    >
                    What am I doing wrong?
                    >
                    Well, just about everything, and most pertinently, asking a question
                    here, the land of the technically-incompetent trolls...but here's how
                    you do it:
                    before making remarks like that you should make *really*
                    sure you havn't made any foolish errors in your code...

                    Oh, sorry I forgot you didn't post any code!


                    In the header file for your library, declare the function as follows:
                    >
                    extern void my_library_func tion(int (*)(const char*));
                    >
                    (Note: as somebody may tell you, "extern" is a redundant
                    linkage specifier for function declarations, but I use it anyway
                    and therefore you should too!)
                    a substantial body of people don't, so consider missing it out.


                    Now write your library function that takes the callback as
                    a parameter in the source file for your library:
                    >
                    void my_library_func tion(int my_callback_fun ction(const char*)) {
                        int my_callback_ret urn;
                        char *my_string;
                    >
                        ... /* generic stuff done here, probably "build up" my_string  */
                    >
                        my_callback_ret urn=my_callback _function(my_st ring);
                    >
                        ... /* more generic stuff maybe, maybe check my_callback_ret urn  */
                        }
                    >
                    Now, for any source file that you want to use that generic
                    my_library_func tion(), you can call it by first #include'ing the
                    library header file, then defining a specific callback function that
                    matches the declaration in the header file:
                    >
                    int my_specific_fun ction(const char* my_string) {
                    >
                        ... /* do something with string, probably print it, right?  */
                        }
                    >
                    Then you can call your library function with the callback anywhere
                    in your source file, as well as any other functions that you have defined
                    that match the callback signature:
                    >
                    void my_function(voi d) {
                    >
                        ... /* stuff happens here, whatever, maybe nothing, who knows  */
                    >
                        my_library_func tion(my_specifi c_function);
                    >
                        ... /* and whatever else  */
                        }
                    >
                    And that's "all" there is to it...not that bad once you get the hang of
                    it, just follow the pattern above,
                    no! don't follow the pattern above!
                    sometimes you have to really "think"
                    about what the perfect "signature" will be for all the various callbacks
                    you want for a generic library function, what all data you need to
                    pass for all possible conditions...
                    no not at all.

                    Ok. Here's a more compact form of your code with a driver added.


                    /***********/
                    /* the code that Bill Reid didn't write */

                    extern void my_library_func tion (int (*)(const char*));

                    void my_library_func tion(int my_callback_fun ction(const char*)) /*
                    <--- error */
                    {
                    int my_callback_ret urn;
                    char *my_string = "";
                    my_callback_ret urn=my_callback _function (my_string);
                    }

                    int my_specific_fun ction (const char* my_string)
                    {
                    return 0;
                    }

                    /* driver added by me */
                    int main (void)
                    {
                    my_library_func tion (my_specific_fu nction);
                    return 0;
                    }
                    /**********/

                    and my compiler does this

                    Compiling...
                    reid.c
                    C:\bin\reid.c(6 ) : warning C4028: formal parameter 1 different from
                    declaration

                    So let's try this pattern:-

                    /* in the header */
                    typedef int (*Callback)(con st char*);
                    void my_library_func tion (Callback);


                    /* in the library */
                    void my_library_func tion (Callback my_callback_fun ction)
                    {
                    int my_callback_ret urn;
                    char *my_string = "";
                    my_callback_ret urn = my_callback_fun ction (my_string);
                    }


                    /* in the caller's code */
                    int my_specific_fun ction (const char* my_string)
                    {
                    return 0;
                    }

                    /* driver added by me */
                    int main (void)
                    {
                    my_library_func tion (my_specific_fu nction);
                    return 0;
                    }


                    the typedef makes life *much* easier.

                    So how to construct the typedef?

                    Suppose the callback is going to look something like this
                    int f1 (int x)

                    put a typedef in front of it and change the name
                    to your convention for types (I start a typename
                    with an uppercase letter)
                    typedef int F1 (int x);

                    put a * in front of the function name and bracket the name
                    and the *.
                    typedef int (*F1) (int x);

                    remove the argument names if you like.
                    typedef int (*F1) (int);

                    You can then use this wherever you need the function
                    pointer.

                    A slightly more complicated example
                    char *f2 (int x, double x, F1 callback);
                    typedef char* (*F2) (int, double, F1);

                    You even return function pointers
                    F1 setCB (F1 new_call);
                    typedef F1 (*SetCB) (F1);


                    Some people prefer to typedef the function then the
                    pointerness is not hidden.

                    typedef int F1 (int);
                    typedef char* F2 (int, double, F1*);

                    And now I'm guilty of Reid's syndrome, I havn't
                    compiled this. But I did compile the pattern
                    I recommend (and use).


                    --
                    Nick Keighley

                    As I recall, OSI dealt with TCP/IP by just admitting it into the spec
                    as a variation of existing levels. This is akin to dealing with an
                    Alien face hugger by allowing it to implant its embryo in your body.

                    Comment

                    • Nick Keighley

                      #11
                      Re: How do I create a function in my library for passing usercallback function

                      On 14 Apr, 08:10, Richard Heathfield <r...@see.sig.i nvalidwrote:
                      Bill Reid said:
                      Richard Heathfield <r...@see.sig.i nvalidwrote in message
                      news:a96dnbUf7a asdp_VRVnygwA@b t.com...
                      Bill Reid said:
                      Angus <nos...@gmail.c omwrote in message
                      news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
                      What am I doing wrong?
                      >
                      Well, just about everything, and most pertinently, asking a question
                      here, the land of the technically-incompetent trolls...but here's how
                      you do it:
                      oh the irony...


                      When you make a claim like that, you should back it up with working
                      code. You didn't. A conforming implementation *must* diagnose, and *may*
                      refuse to translate, your code.
                      >
                      Well, I didn't really write any code, you insane troll, so I guess
                      you're "right" again, as always...
                      >
                      Here is the code you claim you didn't really write, which I've copied
                      verbatim from your article.
                      <snip code-like stuff>


                      Note that my observation about failure to compile does not relate to
                      obvious "more stuff goes here" conventions such as an occasional ellipsis.
                      >
                      Your track record of ignoring or even railing against those who notify
                      you of your mistakes gives me little or no hope that you'll pay any
                      attention to this, but the OP will at least have fair warning of the
                      kind of "competence " to which you are exposing him.
                      >
                      Well, no, I couldn't help but notice that you didn't specify any
                      actual problems with what I posted,
                      >
                      No point. You never listen anyway.
                      >
                      so how would he know what's wrong with it?
                      >
                      Since it doesn't compile, he'll find out pretty quickly that it *is* wrong.
                      As to *why* it's wrong, that's easy. It was written by someone who doesn't
                      understand C very well.
                      >
                      So if you got anything of substance, post it;
                      >
                      After you.
                      perhaps, Richard, if you tried to be just a little less gnomic...
                      Perhaps, even, point out the error :-)

                      Who remembers the Campaign Againt Grumpiness in c.l.c.?


                      --
                      Nick Keighley

                      If cosmology reveals anything about God, it is that He has
                      an inordinate fondness for empty space and non-baryonic dark
                      matter.
                      Sverker Johansson (talk.origins)

                      Comment

                      • Richard Heathfield

                        #12
                        Re: How do I create a function in my library for passing user callback function

                        Nick Keighley said:

                        <snip>
                        perhaps, Richard, if you tried to be just a little less gnomic...
                        Tried that before. It don't work, with Mr Reid.
                        Perhaps, even, point out the error :-)
                        You already done that.
                        Who remembers the Campaign Againt Grumpiness in c.l.c.?
                        It was the Campaign Against *Excessive* Grumpiness, IIRC.

                        And who remembers the Campaign for Grumpiness where Grumpiness is Due?

                        Given Mr Reid's track record, I don't think I'm flouting the membership
                        rules of either of those campaigns.

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

                        • ymuntyan@gmail.com

                          #13
                          Re: How do I create a function in my library for passing usercallback function

                          On Apr 14, 3:49 am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
                          wrote:
                          On 14 Apr, 03:24, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                          >
                          Angus <nos...@gmail.c omwrote in message
                          news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
                          I am writing a library which will write data to a user defined callback
                          function. The function the user of my library will supply is:
                          >
                          int (*callbackfunct ion)(const char*);
                          >
                          In my libary do I create a function where user passes this callback
                          function? How would I define the function?
                          >
                          I tried this
                          >
                          callbackfunctio n clientfunction;
                          >
                          void SpecifyCallback function(cbFunc tion cbFn)
                          >
                          what is cbFunction?
                          >
                          {
                          clientfunction = cbFn;
                          }
                          >
                          Then called like this:
                          clientfunction( sz); // sz is a C-string.
                          >
                          But program crashes with access violation when attempt to call
                          clientfunction
                          >
                          What am I doing wrong?
                          >
                          Well, just about everything, and most pertinently, asking a question
                          here, the land of the technically-incompetent trolls...but here's how
                          you do it:
                          >
                          before making remarks like that you should make *really*
                          sure you havn't made any foolish errors in your code...
                          >
                          Oh, sorry I forgot you didn't post any code!
                          >
                          In the header file for your library, declare the function as follows:
                          >
                          extern void my_library_func tion(int (*)(const char*));
                          >
                          (Note: as somebody may tell you, "extern" is a redundant
                          linkage specifier for function declarations, but I use it anyway
                          and therefore you should too!)
                          >
                          a substantial body of people don't, so consider missing it out.
                          >
                          >
                          >
                          Now write your library function that takes the callback as
                          a parameter in the source file for your library:
                          >
                          void my_library_func tion(int my_callback_fun ction(const char*)) {
                          int my_callback_ret urn;
                          char *my_string;
                          >
                          ... /* generic stuff done here, probably "build up" my_string */
                          >
                          my_callback_ret urn=my_callback _function(my_st ring);
                          >
                          ... /* more generic stuff maybe, maybe check my_callback_ret urn */
                          }
                          >
                          Now, for any source file that you want to use that generic
                          my_library_func tion(), you can call it by first #include'ing the
                          library header file, then defining a specific callback function that
                          matches the declaration in the header file:
                          >
                          int my_specific_fun ction(const char* my_string) {
                          >
                          ... /* do something with string, probably print it, right? */
                          }
                          >
                          Then you can call your library function with the callback anywhere
                          in your source file, as well as any other functions that you have defined
                          that match the callback signature:
                          >
                          void my_function(voi d) {
                          >
                          ... /* stuff happens here, whatever, maybe nothing, who knows */
                          >
                          my_library_func tion(my_specifi c_function);
                          >
                          ... /* and whatever else */
                          }
                          >
                          And that's "all" there is to it...not that bad once you get the hang of
                          it, just follow the pattern above,
                          >
                          no! don't follow the pattern above!
                          >
                          sometimes you have to really "think"
                          about what the perfect "signature" will be for all the various callbacks
                          you want for a generic library function, what all data you need to
                          pass for all possible conditions...
                          >
                          no not at all.
                          >
                          Ok. Here's a more compact form of your code with a driver added.
                          >
                          /***********/
                          /* the code that Bill Reid didn't write */
                          >
                          extern void my_library_func tion (int (*)(const char*));
                          >
                          void my_library_func tion(int my_callback_fun ction(const char*)) /*
                          <--- error */
                          {
                          int my_callback_ret urn;
                          char *my_string = "";
                          my_callback_ret urn=my_callback _function (my_string);
                          >
                          }
                          >
                          int my_specific_fun ction (const char* my_string)
                          {
                          return 0;
                          >
                          }
                          >
                          /* driver added by me */
                          int main (void)
                          {
                          my_library_func tion (my_specific_fu nction);
                          return 0;}
                          >
                          /**********/
                          >
                          and my compiler does this
                          >
                          Compiling...
                          reid.c
                          C:\bin\reid.c(6 ) : warning C4028: formal parameter 1 different from
                          declaration
                          >
                          Is it the error you mentioned? What does it warn about?
                          Parameter is declared as a function, that's adjusted to
                          the pointer to a function. What exactly is wrong?

                          Yevgen

                          Comment

                          • Ben Bacarisse

                            #14
                            Re: How do I create a function in my library for passing user callback function

                            "Bill Reid" <hormelfree@hap pyhealthy.netwr ites:
                            Angus <nospam@gmail.c omwrote in message
                            news:fttgg7$g7k $1$8302bc10@new s.demon.co.uk.. .
                            >>
                            >I am writing a library which will write data to a user defined callback
                            >function.
                            <snip>
                            >But program crashes with access violation when attempt to call
                            >clientfuncti on
                            >>
                            >What am I doing wrong?
                            >
                            Well, just about everything, and most pertinently, asking a question
                            here, the land of the technically-incompetent trolls...but here's how
                            you do it:
                            Posting incorrect syntax won't help the OP. In fact, there is some
                            evidence that the OP knows the correct syntax for what they are doing
                            since the problem they report is a run time one. Only an example of
                            their actual code will help diagnose what they are doing wrong.

                            --
                            Ben.

                            Comment

                            • Nick Keighley

                              #15
                              Re: How do I create a function in my library for passing usercallback function

                              On 14 Apr, 10:34, ymunt...@gmail. com wrote:
                              On Apr 14, 3:49 am,Nick Keighley<nick_k eighley_nos...@ hotmail.com>
                              wrote:
                              >
                              >
                              >
                              >
                              >
                              On 14 Apr, 03:24, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                              >
                              Angus <nos...@gmail.c omwrote in message
                              >news:fttgg7$g7 k$1$8302bc10@ne ws.demon.co.uk. ..
                              I am writing a library which will write data to a user defined callback
                              function.  The function the user of my library will supply is:
                              >
                              int (*callbackfunct ion)(const char*);
                              >
                              In my libary do I create a function where user passes this callback
                              function?  How would I define the function?
                              >
                              I tried this
                              >
                              callbackfunctio n clientfunction;
                              >
                              void SpecifyCallback function(cbFunc tion cbFn)
                              >
                              what is cbFunction?
                              >
                              {
                                 clientfunction = cbFn;
                              }
                              >
                              Then called like this:
                              clientfunction( sz);  // sz is a C-string.
                              >
                              But program crashes with access violation when attempt to call
                              clientfunction
                              >
                              What am I doing wrong?
                              >
                              Well, just about everything, and most pertinently, asking a question
                              here, the land of the technically-incompetent trolls...but here's how
                              you do it:
                              >
                              before making remarks like that you should make *really*
                              sure you havn't made any foolish errors in your code...
                              >
                              Oh, sorry I forgot you didn't post any code!
                              >
                              In the header file for your library, declare the function as follows:
                              >
                              extern void my_library_func tion(int (*)(const char*));
                              >
                              (Note: as somebody may tell you, "extern" is a redundant
                              linkage specifier for function declarations, but I use it anyway
                              and therefore you should too!)
                              >
                              a substantial body of people don't, so consider missing it out.
                              >
                              Now write your library function that takes the callback as
                              a parameter in the source file for your library:
                              >
                              void my_library_func tion(int my_callback_fun ction(const char*)) {
                                  int my_callback_ret urn;
                                  char *my_string;
                              >
                                  ... /* generic stuff done here, probably "build up" my_string  */
                              >
                                  my_callback_ret urn=my_callback _function(my_st ring);
                              >
                                  ... /* more generic stuff maybe, maybe check my_callback_ret urn  */
                                  }
                              >
                              Now, for any source file that you want to use that generic
                              my_library_func tion(), you can call it by first #include'ing the
                              library header file, then defining a specific callback function that
                              matches the declaration in the header file:
                              >
                              int my_specific_fun ction(const char* my_string) {
                              >
                                  ... /* do something with string, probably print it, right?  */
                                  }
                              >
                              Then you can call your library function with the callback anywhere
                              in your source file, as well as any other functions that you have defined
                              that match the callback signature:
                              >
                              void my_function(voi d) {
                              >
                                  ... /* stuff happens here, whatever, maybe nothing, who knows  */
                              >
                                  my_library_func tion(my_specifi c_function);
                              >
                                  ... /* and whatever else  */
                                  }
                              >
                              And that's "all" there is to it...not that bad once you get the hang of
                              it, just follow the pattern above,
                              >
                              no! don't follow the pattern above!
                              >
                              sometimes you have to really "think"
                              about what the perfect "signature" will be for all the various callbacks
                              you want for a generic library function, what all data you need to
                              pass for all possible conditions...
                              >
                              no not at all.
                              >
                              Ok. Here's a more compact form of your code with a driver added.
                              >
                              /***********/
                              /* the code that Bill Reid didn't write */
                              >
                              extern void my_library_func tion (int (*)(const char*));
                              >
                              void my_library_func tion(int my_callback_fun ction(const char*))  /*
                              <--- error */
                              {
                                  int my_callback_ret urn;
                                  char *my_string = "";
                                  my_callback_ret urn=my_callback _function (my_string);
                              >
                              }
                              >
                              int my_specific_fun ction (const char* my_string)
                              {
                                  return 0;
                              >
                              }
                              >
                              /* driver added by me */
                              int main (void)
                              {
                                  my_library_func tion (my_specific_fu nction);
                                  return 0;}
                              >
                              /**********/
                              >
                              and my compiler does this
                              >
                              Compiling...
                              reid.c
                              C:\bin\reid.c(6 ) : warning C4028: formal parameter 1 different from
                              declaration
                              >
                              Is it the error you mentioned?
                              is *what* the error I mentioned? The error above occurs on
                              the line I indicated.

                              What does it warn about?
                              I have this trouble when people ask me to explain syntax errors.
                              To me (maybe I've been at this too long) they seem self explanatory
                              (maybe one day I'll say the same about a three page C++
                              template diagnostic (but not yet)).

                              Well it warns that the formal parameters does not
                              match the declaration. It even tells you which parameter (paramter 1).
                              Which
                              is nice of it but not very helpful as it only has one parameter.

                              The formal parameter appears in the definition
                              void my_library_func tion(int my_callback_fun ction(const char*))

                              so the formal parameter is
                              int my_callback_fun ction(const char*))

                              which is a function. This doesn't make much sense to me.
                              I'm surprised the compiler didn't get more upset.

                              The declaration is:
                              extern void my_library_func tion (int (*)(const char*));

                              so the parameter is
                              int (*)(const char*)

                              Formal is a ptr-to-function-taking-const-char*-and-returning-int

                              So what parameter is a function declaration and the other
                              is a function pointer. They don't match.

                              Parameter is declared as a function, that's adjusted to
                              the pointer to a function. What exactly is wrong?
                              is it?


                              --
                              Nick Keighley


                              Comment

                              Working...