Re: How do I create a function in my library for passing user callback function
- From: John Bode <john_bode@xxxxxxxxxxx>
- Date: Mon, 14 Apr 2008 14:32:48 -0700 (PDT)
On Apr 13, 12:39 pm, "Angus" <nos...@xxxxxxxxx> 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.
But program crashes with access violation when attempt to call
clientfunction
What am I doing wrong?
Based on what you've written here, it sounds like you want to
"register" the callback so it can be called by other functions within
the library: is that correct? If so, here's one approach:
/* Library header file */
#ifndef LIB_H
#define LIB_H
/**
* Create a typedef for the function pointer type
*/
typedef int (*callback)(const char *);
/**
* Register the callback function
*/
void SpecifyCallbackfunction(callback f);
/**
* Other library functions that will use the callback
*/
void L1(const char *foo);
void L2(void);
#endif
/**
* Library implementation file
*/
/**
* File-scope global for callback
*/
static callback g_callback;
void SpecifyCallbackFunction(callback f)
{
g_callback = f;
}
void L1(const char *foo)
{
int bar = g_callback(foo);
...
}
void L2(void)
{
int bar = g_callback("DUMMY");
...
}
/**
* main
*/
#include "library.h"
int blah(const char *bletch)
{
int retVal;
retVal = /* result of doing something with bletch */
return retVal;
}
int main(void)
{
SpecifyCallbackFunction(blah);
L1("blurga");
L2();
...
return 0;
}
HTH.
.
- References:
- Prev by Date: Re: success! I think
- Next by Date: Re: success! I think
- Previous by thread: Re: How do I create a function in my library for passing user callback function
- Next by thread: #define for very small numbers ?
- Index(es):
Relevant Pages
|