Re: How do I create a function in my library for passing user callback function




Wouldn't you know it, this tool posts from Google(TM) Groups...

HEY, FOOL, IF YOU'RE GONNA BE SUCH AN IDIOT AS TO
POST FROM THE SCOURGE OF USENET, AT LEAST LEARN
HOW TO DO IT SO YOUR POSTS ARE QUOTED PROPERLY!!!

TIA!!! (but really, not holding my breath waiting for him to learn
how to post to Usenet...that's too "technical")

Nick Keighley <nick_keighley_nospam@xxxxxxxxxxx> wrote in message
news:ef88a627-7556-4c17-a5ed-c1131d39ba50@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On 14 Apr, 10:34, ymunt...@xxxxxxxxx wrote:
On Apr 14, 3:49 am,Nick Keighley<nick_keighley_nos...@xxxxxxxxxxx>
wrote:
On 14 Apr, 03:24, "Bill Reid" <hormelf...@xxxxxxxxxxxxxxxx> wrote:
Angus <nos...@xxxxxxxxx> wrote in message
news:fttgg7$g7k$1$8302bc10@xxxxxxxxxxxxxxxxxxx

I am writing a library which will write data to a user defined
callback
function. The function the user of my library will supply is:

int (*callbackfunction)(const char*);

In my libary do I create a function where user passes this callback
function? How would I define the function?

I tried this

callbackfunction clientfunction;

void SpecifyCallbackfunction(cbFunction cbFn)

what is cbFunction?

{
clientfunction = cbFn;
}

Then called like this:
clientfunction(sz); // sz is a C-string.

But program crashes with access violation when attempt to call
clientfunction

What am I doing wrong?

Well, just about everything, and most pertinently, asking a question
here, the land of the technically-incompetent trolls...but here's how
you do it:

before making remarks like that you should make *really*
sure you havn't made any foolish errors in your code...

PKB. But let's see how this develops...just don't get all pissy
if it doesn't work out the way you want, like "Little ***" AKA
"troll zero"...remember, we're allegedly trying to "help" a confused
person here, but to do that, we have to "help" each other, right?

Oh, sorry I forgot you didn't post any code!

I'll correct that "error" now...

In the header file for your library, declare the function as follows:

extern void my_library_function(int (*)(const char*));

(Note: as somebody may tell you, "extern" is a redundant
linkage specifier for function declarations, but I use it anyway
and therefore you should too!)

a substantial body of people don't, so consider missing it out.

Maybe it's a mistake to NOT include it...I know this is hard,
but THINK...

Now write your library function that takes the callback as
a parameter in the source file for your library:

void my_library_function(int my_callback_function(const char*)) {
int my_callback_return;
char *my_string;

... /* generic stuff done here, probably "build up" my_string */

my_callback_return=my_callback_function(my_string);

... /* more generic stuff maybe, maybe check my_callback_return */
}

Now, for any source file that you want to use that generic
my_library_function(), you can call it by first #include'ing the
library header file, then defining a specific callback function that
matches the declaration in the header file:

int my_specific_function(const char* my_string) {

... /* do something with string, probably print it, right? */
}

Then you can call your library function with the callback anywhere
in your source file, as well as any other functions that you have
defined
that match the callback signature:

void my_function(void) {

... /* stuff happens here, whatever, maybe nothing, who knows */

my_library_function(my_specific_function);

... /* and whatever else */
}

And that's "all" there is to it...not that bad once you get the hang
of
it, just follow the pattern above,

no! don't follow the pattern above!

Why not? Works for me...what's the disconnect?

sometimes you have to really "think"
about what the perfect "signature" will be for all the various
callbacks
you want for a generic library function, what all data you need to
pass for all possible conditions...

no not at all.

OK, you're right, you don't have to THINK about anything at all
when programming...oh, that's right, you DO have to think when
programming, just not when posting to the Internet...

Ok. Here's a more compact form of your code with a driver added.

/***********/
/* the code that Bill Reid didn't write */

extern void my_library_function (int (*)(const char*));

void my_library_function(int my_callback_function(const char*)) /*
<--- error */
{
int my_callback_return;
char *my_string = "";
my_callback_return=my_callback_function (my_string);

}

int my_specific_function (const char* my_string)
{
return 0;

}

/* driver added by me */
int main (void)
{
my_library_function (my_specific_function);
return 0;}

/**********/

and my compiler does this

Compiling...
reid.c
C:\bin\reid.c(6) : warning C4028: formal parameter 1 different from
declaration

Is it the error you mentioned?

--- unquoted tool text ---
is *what* the error I mentioned? The error above occurs on
the line I indicated.
--- end tool text

He's asking you why your compiler reports an error, since his
may not under the same circumstances, you massive tool...

What does it warn about?

--- unquoted tool text ---
I have this trouble when people ask me to explain syntax errors.
To me (maybe I've been at this too long) they seem self explanatory
(maybe one day I'll say the same about a three page C++
template diagnostic (but not yet)).
--- end tool text

IBID (what a total tool this idiot is..)

--- unquoted tool text ---
Well it warns that the formal parameters does not
match the declaration. It even tells you which parameter (paramter 1).
Which
is nice of it but not very helpful as it only has one parameter.

The formal parameter appears in the definition
void my_library_function(int my_callback_function(const char*))

so the formal parameter is
int my_callback_function(const char*))

which is a function. This doesn't make much sense to me.
I'm surprised the compiler didn't get more upset.

The declaration is:
extern void my_library_function (int (*)(const char*));

so the parameter is
int (*)(const char*)

Formal is a ptr-to-function-taking-const-char*-and-returning-int

So what parameter is a function declaration and the other
is a function pointer. They don't match.
--- end tool text

Parameter is declared as a function, that's adjusted to
the pointer to a function. What exactly is wrong?

--- unquoted tool text ---
is it?
-- end tool text

Yes, you total tool, that's his conception about how function
pointers work, and basically mine...so here's my REALLY
compact version of the code THAT COMPILES COMPLETELY
CLEANLY FOR ME:

library_h.h

/* library_h.h */
/* library header file */

extern void my_library_function (int (*)(const char*));

library_c.c

/* library_c.c */
/* library "C" source */

#include "library_h.h"

void my_library_function(int my_callback_function(const char*)) {

}

I can compile library_c.c to library_c.obj without so much as
a warning, error, scolding, and then proceed likewise to build
library.lib...so in the interest of everybody learning something
today, other than you're a tool who doesn't know how to post
to Usenet, EXPLAIN THE DISCREPANCY..."spec lawyer" it,
tell us exactly what you did differently, whatever, just clear up
the confusion here...

---
William Ernest Reid



.