question on function pointers

D

Durango

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

Nick Keighley

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
 
B

Barry Schwarz

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

Barry Schwarz

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

Eric Sosman

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

Morris Keesan

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

Nick Keighley

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*?
 
J

James Kuyper

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*.
 
M

Morris Keesan

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 *).
 
P

Pavel

Durango said:
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
 
D

Dann Corbit

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top