question on function pointers

Discussion in 'C Programming' started by Durango, Dec 4, 2011.

  1. Durango

    Durango Guest

    Hello I have another question this one regards function pointers.
    I have an api function that has the following prototype.

    int sqlite3_exec(
    sqlite3*, /* An open database */
    const char *sql, /* SQL to be evaluated */
    int (*callback)(void*,int,char**,char**), /* Callback function */
    void *, /* 1st argument to callback*/
    char **errmsg /* Error msg written here */
    );

    the callback function takes a void pointer for the argument and returns
    an integer used for sqlite return code.

    I need to be able to return values from the callback function however I
    am not sure how I can do that. I have heard that u can pass a function
    pointer as the argument to be able to this but I am not sure how this is
    done.
    If anyone can help me with this I would be greatful. I have spent a long
    time trying to figure this out on my own I have to go sleep now.

    thank you.
    Durango, Dec 4, 2011
    #1
    1. Advertising

  2. On Dec 4, 10:33 am, Durango <> wrote:

    > Hello I have another question this one regards function pointers.
    > I have an api function that has the following prototype.
    >
    > int sqlite3_exec(
    >   sqlite3*,                                 /* An open database */
    >   const char *sql,                           /* SQL to be evaluated */
    >   int (*callback)(void*,int,char**,char**),  /* Callback function */
    >   void *,                                    /* 1st argument to callback*/
    >   char **errmsg                             /* Error msg written here */
    > );
    >
    > the callback function takes a void pointer for the argument and returns
    > an integer used for sqlite return code.


    no. The callback function takes a void*, an int and two char** as
    arguments

    > I need to be able to return values from the callback function


    isn't this a callback function that returs an int?

    int my_callback (void* vp, int i, char** cpp1, char** cpp2)
    {
    return 1;
    }

    > however I
    > am not sure how I can do that.  I have heard that u can pass a function
    > pointer as the argument to be able to this but I am not sure how this is
    > done.


    nor me. Nor can I see how that helps you "return an int". You *could*
    stuff a functio pointer into the void* but I'm pretty sure that's
    undefined behaviour. And again I don't see how this helps you.

    > If anyone can help me with this I would be greatful.  I have spent a long
    > time trying to figure this out on my own I have to go sleep now.


    read the documentaion for sqlite3_exec() or tell us what it's supposed
    to do. I don't think the other posters suggestion that you chnage the
    signature of sqlite3_exec() is going to wok because I suspect its part
    of a fixed API that you may not be able to tamper with.

    Basically we need more information about what you are trying to do
    Nick Keighley, Dec 4, 2011
    #2
    1. Advertising

  3. On Sun, 04 Dec 2011 05:05:13 -0600, Robert Wessel
    <> wrote:

    >On 04 Dec 2011 10:33:06 GMT, Durango <> wrote:
    >
    >>Hello I have another question this one regards function pointers.
    >>I have an api function that has the following prototype.
    >>
    >>int sqlite3_exec(
    >> sqlite3*, /* An open database */
    >> const char *sql, /* SQL to be evaluated */
    >> int (*callback)(void*,int,char**,char**), /* Callback function */
    >> void *, /* 1st argument to callback*/
    >> char **errmsg /* Error msg written here */
    >>);
    >>
    >>the callback function takes a void pointer for the argument and returns
    >>an integer used for sqlite return code.
    >>
    >>I need to be able to return values from the callback function however I
    >>am not sure how I can do that. I have heard that u can pass a function
    >>pointer as the argument to be able to this but I am not sure how this is
    >>done.
    >>If anyone can help me with this I would be greatful. I have spent a long
    >>time trying to figure this out on my own I have to go sleep now.

    >
    >Just add an additional pointer to the callback function. Return your
    >additional data to the area so pointed to.


    Since the callback function will be called sqlite3_exec, it is
    unlikely that any additional arguments will be passed.

    --
    Remove del for email
    Barry Schwarz, Dec 4, 2011
    #3
  4. On 04 Dec 2011 10:33:06 GMT, Durango <> wrote:

    >Hello I have another question this one regards function pointers.
    >I have an api function that has the following prototype.
    >
    >int sqlite3_exec(
    > sqlite3*, /* An open database */
    > const char *sql, /* SQL to be evaluated */
    > int (*callback)(void*,int,char**,char**), /* Callback function */
    > void *, /* 1st argument to callback*/
    > char **errmsg /* Error msg written here */
    >);
    >
    >the callback function takes a void pointer for the argument and returns
    >an integer used for sqlite return code.
    >
    >I need to be able to return values from the callback function however I
    >am not sure how I can do that. I have heard that u can pass a function
    >pointer as the argument to be able to this but I am not sure how this is
    >done.
    >If anyone can help me with this I would be greatful. I have spent a long
    >time trying to figure this out on my own I have to go sleep now.


    The callback function is already declared to return a single int. You
    indicate you need to return multiple values. Since you probably have
    no control over how the function is called, you cannot change its
    signature to create additional pointers you could use to identify
    objects to contain these additional values. The only option I can
    think of is to define the objects that will hold theses additional
    values at file scope and your callback function will be able to access
    them by name.

    --
    Remove del for email
    Barry Schwarz, Dec 4, 2011
    #4
  5. Durango

    Eric Sosman Guest

    On 12/4/2011 5:33 AM, Durango wrote:
    > Hello I have another question this one regards function pointers.
    > I have an api function that has the following prototype.
    >
    > int sqlite3_exec(
    > sqlite3*, /* An open database */
    > const char *sql, /* SQL to be evaluated */
    > int (*callback)(void*,int,char**,char**), /* Callback function */
    > void *, /* 1st argument to callback*/
    > char **errmsg /* Error msg written here */
    > );
    >
    > the callback function takes a void pointer for the argument and returns
    > an integer used for sqlite return code.


    No, the callback function takes four arguments, not one.
    In addition to the void* you mention, there are also an int
    and two char**.

    If the function you actually provide does not have this
    same "signature" -- four arguments of the stated types, return
    value an int -- then the behavior is undefined. (On the reasonable
    assumption that the callback's caller uses the function pointer
    "as advertised.") If you're just plugging in some other kind of
    function, it's not guaranteed to do anything sensible.

    > I need to be able to return values from the callback function however I
    > am not sure how I can do that. I have heard that u can pass a function
    > pointer as the argument to be able to this but I am not sure how this is
    > done.


    The callback function takes three arguments that are pointers.
    Since none of them point to const objects, the callback could, at
    least in principle, store something where the pointers point. Of
    course, this depends on what the calling-back framework provides,
    and what significance it attaches to those pointed-to things. Read
    the documentation for the framework.

    One glimmer of hope: A *very* common pattern in callback-style
    frameworks is to allow the user to "smuggle" information to the
    callback function. Usually, you call the framework and pass it a
    blob of data that the framework itself doesn't use, and the framework
    dutifully passes that same blob to the callback function. Thus, the
    ultimate caller and the callback function can communicate "through"
    the intermediating framework. And what's the most commonly-used
    type for such a blob? Why, a void* -- precisely because it can point
    to (nearly) anything at all. So when you're reading the framework
    documentation, the first thing to look at might well be the callback
    function's first argument: It may exist just so you can take advantage
    of it in this way.

    --
    Eric Sosman
    d
    Eric Sosman, Dec 4, 2011
    #5
  6. On Sun, 04 Dec 2011 06:34:29 -0500, Nick Keighley
    <> wrote:

    > On Dec 4, 10:33 am, Durango <> wrote:

    ....
    >>  I have heard that u can pass a function
    >> pointer as the argument to be able to this but I am not sure how this is
    >> done.

    >
    > You *could*
    > stuff a function pointer into the void* but I'm pretty sure that's
    > undefined behaviour.


    But it would be easy to avoid the undefined behavior by adding one
    more level of indirections, and stuffing the address of a
    function pointer into the void*.

    void myfunc(void) { }
    void (*myfuncptr)(void) = &myfunc;
    void *param = &myfuncptr;


    ....
    > Basically we need more information about what you are trying to do


    Agreed.

    --
    Morris Keesan --
    Morris Keesan, Dec 4, 2011
    #6
  7. On Dec 4, 5:20 pm, "Morris Keesan" <> wrote:
    > On Sun, 04 Dec 2011 06:34:29 -0500, Nick Keighley
    >
    >
    >
    > <> wrote:
    > > On Dec 4, 10:33 am, Durango <> wrote:

    > ...
    > >>  I have heard that u can pass a function
    > >> pointer as the argument to be able to this but I am not sure how this is
    > >> done.

    >
    > > You *could*
    > > stuff a function pointer into the void* but I'm pretty sure that's
    > > undefined behaviour.

    >
    > But it would be easy to avoid the undefined behavior by adding one
    > more level of indirections, and stuffing the address of a
    > function pointer into the void*.
    >
    > void myfunc(void) { }
    > void (*myfuncptr)(void) = &myfunc;
    > void *param = &myfuncptr;


    is that well defined behaviour? I thought a function pointer was
    incompatible with a void*?
    Nick Keighley, Dec 4, 2011
    #7
  8. Durango

    James Kuyper Guest

    On 12/04/2011 12:25 PM, Nick Keighley wrote:
    > On Dec 4, 5:20�pm, "Morris Keesan" <> wrote:

    ....
    >> void myfunc(void) { }
    >> void (*myfuncptr)(void) = &myfunc;
    >> void *param = &myfuncptr;

    >
    > is that well defined behaviour? I thought a function pointer was
    > incompatible with a void*?


    The object whose address he's passing contains a function pointer, but
    it's own address is an object pointer type, and as such, is safely
    convertible to void*.
    --
    James Kuyper
    James Kuyper, Dec 4, 2011
    #8
  9. On Sun, 04 Dec 2011 12:25:45 -0500, Nick Keighley
    <> wrote:

    > On Dec 4, 5:20 pm, "Morris Keesan" <> wrote:
    >> On Sun, 04 Dec 2011 06:34:29 -0500, Nick Keighley
    >>
    >>
    >>
    >> <> wrote:
    >> > On Dec 4, 10:33 am, Durango <> wrote:

    >> ...
    >> >>  I have heard that u can pass a function
    >> >> pointer as the argument to be able to this but I am not sure how

    >> this is
    >> >> done.

    >>
    >> > You *could*
    >> > stuff a function pointer into the void* but I'm pretty sure that's
    >> > undefined behaviour.

    >>
    >> But it would be easy to avoid the undefined behavior by adding one
    >> more level of indirections, and stuffing the address of a
    >> function pointer into the void*.
    >>
    >> void myfunc(void) { }
    >> void (*myfuncptr)(void) = &myfunc;
    >> void *param = &myfuncptr;

    >
    > is that well defined behaviour? I thought a function pointer was
    > incompatible with a void*?


    The function pointer myfuncptr above is not compatible with a (void *),
    but &myfuncptr is a pointer to that pointer, not a pointer to function,
    so it can safely be converted to and from (void *).
    --
    Morris Keesan --
    Morris Keesan, Dec 5, 2011
    #9
  10. Durango

    Pavel Guest

    Durango wrote:
    > Hello I have another question this one regards function pointers.
    > I have an api function that has the following prototype.
    >
    > int sqlite3_exec(
    > sqlite3*, /* An open database */
    > const char *sql, /* SQL to be evaluated */
    > int (*callback)(void*,int,char**,char**), /* Callback function */
    > void *, /* 1st argument to callback*/
    > char **errmsg /* Error msg written here */
    > );
    >
    > the callback function takes a void pointer for the argument and returns
    > an integer used for sqlite return code.
    >
    > I need to be able to return values from the callback function however I
    > am not sure how I can do that. I have heard that u can pass a function
    > pointer as the argument to be able to this but I am not sure how this is
    > done.
    > If anyone can help me with this I would be greatful. I have spent a long
    > time trying to figure this out on my own I have to go sleep now.
    >
    > thank you.

    If you are free to determine the actual type of the pointee of that /*1st
    argument to callback*/, just define it to a structure that contains what it
    contains for you now plus the duplicate of the callback's return value and store
    it there. For example:
    your current 1st argument structure:
    struct S {
    int mySomeData;
    };
    your new 1st argument structure:
    struct S {
    int mySomeData;
    int returnCode;
    };

    Your current callback:
    int myCallBack(void *arg, ...) {
    struct S *sArg = (struct S *)arg;
    ....
    return 5;
    }

    Your new callback:
    int myCallBack(void *arg, ...) {
    struct S *sArg = (struct S *)arg;
    ....
    sArg->returnCode = 5;
    return 5;
    }

    Then you can check returnCode outside of sqlite3_exec() after it returns.

    HTH
    Pavel
    Pavel, Dec 5, 2011
    #10
  11. Durango

    Dann Corbit Guest

    In article <4edb4c62$0$294$>,
    says...
    >
    > Hello I have another question this one regards function pointers.
    > I have an api function that has the following prototype.
    >
    > int sqlite3_exec(
    > sqlite3*, /* An open database */
    > const char *sql, /* SQL to be evaluated */
    > int (*callback)(void*,int,char**,char**), /* Callback function */
    > void *, /* 1st argument to callback*/
    > char **errmsg /* Error msg written here */
    > );
    >
    > the callback function takes a void pointer for the argument and returns
    > an integer used for sqlite return code.
    >
    > I need to be able to return values from the callback function however I
    > am not sure how I can do that. I have heard that u can pass a function
    > pointer as the argument to be able to this but I am not sure how this is
    > done.
    > If anyone can help me with this I would be greatful. I have spent a long
    > time trying to figure this out on my own I have to go sleep now.


    http://stackoverflow.com/questions/1805982/use-of-sqlite3-exec
    Or:
    http://www.adp-gmbh.ch/sqlite/embed_sqlite.html
    Or:
    http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html
    and many others, easily found via web search.
    Dann Corbit, Dec 6, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    637
    llewelly
    Sep 16, 2003
  2. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    502
    Peter Goddard
    May 16, 2005
  3. Chad

    pointers to pointers question

    Chad, Mar 26, 2006, in forum: C Programming
    Replies:
    5
    Views:
    302
    Default User
    Mar 27, 2006
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    458
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
Loading...

Share This Page