Can someone please explain why I needed to add a second definition of a callback fct?
I was writing some code for a sqlite3 application and wanted to use this code
PHP Code:
Code:
rc = sqlite3_exec(db, select, process_sql, db, &errmsg); When I replaced that code with this Code:
int (*callback)(); My problem is that, as far as I can see, both calls should have been identical.:scratch: Is it, perhaps, some optimization changing the "standard" function definition? Here's the (very simple) compile line I was using: Code:
gcc comb3.c -lsqlite3 -o comb3 |
Your argument list doesn't agree with the argument list defined by sqlite3 (the first argument is a void *)
Code:
typedef int (*sqlite_callback)(void*,int,char**, char**); Code:
rc = sqlite3_exec(db, select, (sqlite3_callback) process_sql, db, &errmsg); |
Agreed, the first argument is typed as a void pointer to enable one to pass a pointer to anything into the call back function. But the "type mismatch" compile error specifically referred to the third argument.
Note that the compiler did not complain about Code:
int (*callback)(); Note also that the specification of the sqlite3_exec function: Code:
int sqlite3_exec( Code:
typedef int (*sqlite3_callback)(void*,int,char**, char**); Code:
static int process_sql( // Return 0 on success Code:
static int callback(void *NotUsed, int argc, char **argv, char **azColName){ Code:
comb4.c: In function ‘main’: So I'm still puzzled. :scratch: |
Hi -
The problem (as you verified) is very definitely not the "static". It's just weirdness with C/C++ function pointer syntax. Here's a link that might help: http://publications.gbdirect.co.uk/c..._pointers.html |
Hum. O.K., I think I've got it after looking at paulsm's reference. (Thanks Paul.)
The function declaration int foo() {...} tells the compiler to generate code to invoke foo when it sees a foo() somewhere in the code. But a pointer to foo has to be declared as int (*foo)() and so my use of an unqualified foo in the sqlite3_callback function was not correct. The construct suggested by gramef works because it instructs the compile to use a pointer to the function. Unfortunately that "explanation" seems to accurately convey my still confused state. Can anyone do it any more clearly? |
Hi, PTrenholme -
You're exactly right. The syntax *is* confusing - but this statement is absolutely correct: Quote:
'Hope that helps .. PSM |
I notice that you are getting a warning not an error.
What happens if you declare process_sql to exactly match with sqlite3_callback (ie with void*) and then typecast within the function to a sqlite*? |
graemef, since the warning was generated by the calling program, not the called one, I tried
Code:
rc = sqlite3_exec(db, select, (void *) process_sql, db, &errmsg); Of course your original suggestion: Code:
rc = sqlite3_exec(db, select, (sqlite3_callback) process_sql, db, &errmsg); But, at the point in the compilation where the warning is being generated the types of the arguments of the callback function are irrelevant since all that's being passes is a pointer to a function. Thus changing the type of the "pointer passed as the first argument to the callback function" in the callback function declaration has no effect on the warning message. By the way, if you're interested in the complete program it's posted here. It's a app to take a string of characters from the standard input and write all possible permutations of all substrings of the input characters (of two characters or more), to the standard output. The OP in that thread was trying to debug an app that was intended to find all words that could be generated by rearranging the letters in an input word using a home-grown linked list program. I was trying to show the OP that reinventing the wheel was not always the best way to go. . . |
All times are GMT -5. The time now is 05:16 PM. |