Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question on function pointers

Reply
Thread Tools

question on function pointers

 
 
Durango
Guest
Posts: n/a
 
      12-04-2011
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.
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      12-04-2011
On Dec 4, 10:33*am, Durango <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      12-04-2011
On Sun, 04 Dec 2011 05:05:13 -0600, Robert Wessel
<(E-Mail Removed)> wrote:

>On 04 Dec 2011 10:33:06 GMT, Durango <(E-Mail Removed)> 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-04-2011
On 04 Dec 2011 10:33:06 GMT, Durango <(E-Mail Removed)> 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-04-2011
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      12-04-2011
On Sun, 04 Dec 2011 06:34:29 -0500, Nick Keighley
<(E-Mail Removed)> wrote:

> On Dec 4, 10:33*am, Durango <(E-Mail Removed)> 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 -- (E-Mail Removed)
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-04-2011
On Dec 4, 5:20*pm, "Morris Keesan" <(E-Mail Removed)> wrote:
> On Sun, 04 Dec 2011 06:34:29 -0500, Nick Keighley
>
>
>
> <(E-Mail Removed)> wrote:
> > On Dec 4, 10:33*am, Durango <(E-Mail Removed)> 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*?


 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-04-2011
On 12/04/2011 12:25 PM, Nick Keighley wrote:
> On Dec 4, 5:20�pm, "Morris Keesan" <(E-Mail Removed)> 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
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      12-05-2011
On Sun, 04 Dec 2011 12:25:45 -0500, Nick Keighley
<(E-Mail Removed)> wrote:

> On Dec 4, 5:20*pm, "Morris Keesan" <(E-Mail Removed)> wrote:
>> On Sun, 04 Dec 2011 06:34:29 -0500, Nick Keighley
>>
>>
>>
>> <(E-Mail Removed)> wrote:
>> > On Dec 4, 10:33*am, Durango <(E-Mail Removed)> 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 -- (E-Mail Removed)
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      12-05-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
pointers to pointers question Chad C Programming 5 03-27-2006 07:44 AM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments