question on function pointers

This is a discussion on question on function pointers within the C Programming forums, part of the General Programming Boards category; Hello I have another question this one regards function pointers. I have an api function that has the following prototype. ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    question on function pointers

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


    Code:
    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.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    You just define your function with the same signature as the pointer:
    Code:
    int my_callback(void *ptr1, int num, char **ptr2, char **ptr3)
    {
      // Code goes here
    }
    Then you just pass the name of your function in as the parameter:
    Code:
    sqlite3_exec(sqlite, "blah blah blah", my_callback, ptr1, "You messed up!");
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    I understand this, my question is that since the callback function returns an integer and I need to retrieve a struct from it, how can I do this? So basically is there a way I can pass a pointer as argument where I can save my values and be able to retrieve them from the pointer outside of the function? the argument takes a void pointer so I will have to cast it and assign it to another variable. I assume defining a global variable pointer to a struct will work but not sure.

    Code:
    static struct test *arg;
    
    int main()
    {
       struct test *q;
       q = malloc(sizeof(struct test));
       /* allocate memory for any further values needed for struct */
       sqlite3_exec(sqlite, sqlstmt, my_callback, &q, &error);
    }
    
    int my_callback(void *ptr1, int num, char **ptr2, char **ptr3)
    {
       arg = (struct test *)ptr1;
       /* update arg values */
    }
    


    I have not tried this yet but I assume it should do the job if you have any suggestions please let me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on Function Pointers
    By rebelstar in forum C Programming
    Replies: 5
    Last Post: 09-26-2011, 09:42 PM
  2. pointers to function question
    By -EquinoX- in forum C Programming
    Replies: 31
    Last Post: 04-29-2008, 01:06 PM
  3. Question about my trim function and pointers
    By space-oddity in forum C Programming
    Replies: 10
    Last Post: 04-28-2008, 01:22 AM
  4. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 08:21 PM
  5. Linked List, pointers, function question
    By Canucklehead in forum C++ Programming
    Replies: 9
    Last Post: 11-02-2005, 12:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21