Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How do I create a function in my library for passing user callback function

Reply
Thread Tools

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

 
 
Angus
Guest
Posts: n/a
 
      04-13-2008
Hello

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)
{
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?


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-13-2008
Angus wrote:
> Hello
>
> 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)
> {
> clientfunction = cbFn;
> }
>
> Then called like this:
> clientfunction(sz); // sz is a C-string.
>

You should be passing the address of the function, not a string.

int f( const char* );

clientfunction( f );

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-13-2008
Richard Heathfield wrote:
> Ian Collins said:
>
>> Angus wrote:
>>> Hello
>>>
>>> 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)
>>> {
>>> clientfunction = cbFn;
>>> }
>>>
>>> Then called like this:
>>> clientfunction(sz); // sz is a C-string.
>>>

>> You should be passing the address of the function, not a string.
>>
>> int f( const char* );
>>
>> clientfunction( f );

>
> I doubt it. Since clientfunction is an instance of callbackfunction (and
> presumably the definition of callbackfunction, above, is supposed to be a
> typedef), it takes a const char *, not an int(*)(const char *).
>

OK, I promise never to post pre-caffeine ever again!

I read the OP as passing a string to SpecifyCallbackfunction.

--
Ian Collins.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-13-2008
"Angus" <(E-Mail Removed)> writes:

> 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;


This won't compile. I suspect you have a typedef that you are not
showing us!

<snip>
> But program crashes with access violation when attempt to call
> clientfunction
>
> What am I doing wrong?


Try to post a short, compilable, example of the problem. The outline
you posted is sound, the error is in the detail (and is somewhere
else).

--
Ben.
 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      04-14-2008

Angus <(E-Mail Removed)> wrote in message
news:fttgg7$g7k$1$(E-Mail Removed)...

> Hello
>
> 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)
> {
> 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:

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!)

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

---
William Ernest Reid



 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      04-14-2008

Richard Heathfield <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bill Reid said:
>
> > Angus <(E-Mail Removed)> wrote in message
> > news:fttgg7$g7k$1$(E-Mail Removed)...
> >

> <snip>
> >>
> >> 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:

>
> When you make a claim like that, you should back it up with working code.
> You didn't. A conforming implementation *must* diagnose, and *may* refuse
> to translate, your code.


Well, I didn't really write any code, you insane troll, so I guess
you're "right" again, as always...

> Your track record of ignoring or even railing against those who notify you
> of your mistakes gives me little or no hope that you'll pay any attention
> to this, but the OP will at least have fair warning of the kind of
> "competence" to which you are exposing him.


Well, no, I couldn't help but notice that you didn't specify any
actual problems with what I posted, so how would he know what's
wrong with it? Believe me, for his sake, you should let HIM know,
and let ALL of us know, including me, so we can all benefit from
your tremendous "wisdom"...I'd be the first to admit that I make
a LOT of mistakes, have made many here, and there may be
errors or omissions in my post, and I'd genuinely like to be
"set straight", but somehow I think you're just blowin' troll
smoke again, as usual...

So if you got anything of substance, post it; I'm not saying it
would be a first, but along the lines of a rarity...

---
William Ernest Reid



 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      04-14-2008
On 14 Apr, 03:24, "Bill Reid" <(E-Mail Removed)> wrote:
> Angus <(E-Mail Removed)> wrote in message
> news:fttgg7$g7k$1$(E-Mail Removed)...



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

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



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



> 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!

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

So let's try this pattern:-

/* in the header */
typedef int (*Callback)(const char*);
void my_library_function (Callback);


/* in the library */
void my_library_function (Callback my_callback_function)
{
int my_callback_return;
char *my_string = "";
my_callback_return = my_callback_function (my_string);
}


/* in the caller's code */
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;
}


the typedef makes life *much* easier.

So how to construct the typedef?

Suppose the callback is going to look something like this
int f1 (int x)

put a typedef in front of it and change the name
to your convention for types (I start a typename
with an uppercase letter)
typedef int F1 (int x);

put a * in front of the function name and bracket the name
and the *.
typedef int (*F1) (int x);

remove the argument names if you like.
typedef int (*F1) (int);

You can then use this wherever you need the function
pointer.

A slightly more complicated example
char *f2 (int x, double x, F1 callback);
typedef char* (*F2) (int, double, F1);

You even return function pointers
F1 setCB (F1 new_call);
typedef F1 (*SetCB) (F1);


Some people prefer to typedef the function then the
pointerness is not hidden.

typedef int F1 (int);
typedef char* F2 (int, double, F1*);

And now I'm guilty of Reid's syndrome, I havn't
compiled this. But I did compile the pattern
I recommend (and use).


--
Nick Keighley

As I recall, OSI dealt with TCP/IP by just admitting it into the spec
as a variation of existing levels. This is akin to dealing with an
Alien face hugger by allowing it to implant its embryo in your body.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      04-14-2008
On 14 Apr, 08:10, Richard Heathfield <(E-Mail Removed)> wrote:
> Bill Reid said:
> > Richard Heathfield <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...
> >> Bill Reid said:
> >> > Angus <(E-Mail Removed)> wrote in message
> >> >news:fttgg7$g7k$1$(E-Mail Removed)...


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


oh the irony...



> >> When you make a claim like that, you should back it up with working
> >> code. You didn't. A conforming implementation *must* diagnose, and *may*
> >> refuse to translate, your code.

>
> > Well, I didn't really write any code, you insane troll, so I guess
> > you're "right" again, as always...

>
> Here is the code you claim you didn't really write, which I've copied
> verbatim from your article.


<snip code-like stuff>



> Note that my observation about failure to compile does not relate to
> obvious "more stuff goes here" conventions such as an occasional ellipsis.
>
> >> Your track record of ignoring or even railing against those who notify
> >> you of your mistakes gives me little or no hope that you'll pay any
> >> attention to this, but the OP will at least have fair warning of the
> >> kind of "competence" to which you are exposing him.

>
> > Well, no, I couldn't help but notice that you didn't specify any
> > actual problems with what I posted,

>
> No point. You never listen anyway.
>
> > so how would he know what's wrong with it?

>
> Since it doesn't compile, he'll find out pretty quickly that it *is* wrong.
> As to *why* it's wrong, that's easy. It was written by someone who doesn't
> understand C very well.
>
> > So if you got anything of substance, post it;

>
> After you.


perhaps, Richard, if you tried to be just a little less gnomic...
Perhaps, even, point out the error

Who remembers the Campaign Againt Grumpiness in c.l.c.?


--
Nick Keighley

If cosmology reveals anything about God, it is that He has
an inordinate fondness for empty space and non-baryonic dark
matter.
Sverker Johansson (talk.origins)
 
Reply With Quote
 
ymuntyan@gmail.com
Guest
Posts: n/a
 
      04-14-2008
On Apr 14, 3:49 am, Nick Keighley <(E-Mail Removed)>
wrote:
> On 14 Apr, 03:24, "Bill Reid" <(E-Mail Removed)> wrote:
>
> > Angus <(E-Mail Removed)> wrote in message
> >news:fttgg7$g7k$1$(E-Mail Removed)...
> > > 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...
>
> Oh, sorry I forgot you didn't post any code!
>
> > 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.
>
>
>
> > 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!
>
> > 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. 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? What does it warn about?
Parameter is declared as a function, that's adjusted to
the pointer to a function. What exactly is wrong?

Yevgen
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-14-2008
"Bill Reid" <(E-Mail Removed)> writes:

> Angus <(E-Mail Removed)> wrote in message
> news:fttgg7$g7k$1$(E-Mail Removed)...
>>
>> I am writing a library which will write data to a user defined callback
>> function.

<snip>
>> 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:


Posting incorrect syntax won't help the OP. In fact, there is some
evidence that the OP knows the correct syntax for what they are doing
since the problem they report is a run time one. Only an example of
their actual code will help diagnose what they are doing wrong.

--
Ben.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Passing static member function as callback to Windows DLL Evan Burkitt C++ 2 08-24-2007 10:37 PM
Passing arguments to callback function in addEventListener Janus Javascript 10 01-11-2007 01:23 PM
SWIG -- Passing python proxy class instance to python callback Jeff Python 0 05-02-2005 02:55 PM
passing callback function to c-extension Diez B. Roggisch Python 4 01-14-2004 06:25 PM



Advertisments