|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 *). > -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"Angus" <nospam@gmail.com> 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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Angus <nospam@gmail.com> wrote in message news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > 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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Bill Reid said:
> Angus <nospam@gmail.com> wrote in message > news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > <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. 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. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Richard Heathfield <rjh@see.sig.invalid> wrote in message news:a96dnbUf7aasdp_VRVnygwA@bt.com... > Bill Reid said: > > > Angus <nospam@gmail.com> wrote in message > > news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > > > <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 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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Bill Reid said:
> > Richard Heathfield <rjh@see.sig.invalid> wrote in message > news:a96dnbUf7aasdp_VRVnygwA@bt.com... >> Bill Reid said: >> >> > Angus <nospam@gmail.com> wrote in message >> > news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... >> > >> <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... Here is the code you claim you didn't really write, which I've copied verbatim from your article. extern void my_library_function(int (*)(const char*)); 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 */ } int my_specific_function(const char* my_string) { ... /* do something with string, probably print it, right? */ } void my_function(void) { ... /* stuff happens here, whatever, maybe nothing, who knows */ my_library_function(my_specific_function); ... /* and whatever else */ } 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 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. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On 14 Apr, 03:24, "Bill Reid" <hormelf...@happyhealthy.net> wrote:
> Angus <nos...@gmail.com> wrote in message > news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > > 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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
On 14 Apr, 08:10, Richard Heathfield <r...@see.sig.invalid> wrote:
> Bill Reid said: > > Richard Heathfield <r...@see.sig.invalid> wrote in message > >news:a96dnbUf7aasdp_VRVnygwA@bt.com... > >> Bill Reid said: > >> > Angus <nos...@gmail.com> wrote in message > >> >news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > >> >> 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 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) |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
Nick Keighley said:
<snip> > perhaps, Richard, if you tried to be just a little less gnomic... Tried that before. It don't work, with Mr Reid. > Perhaps, even, point out the error :-) You already done that. > Who remembers the Campaign Againt Grumpiness in c.l.c.? It was the Campaign Against *Excessive* Grumpiness, IIRC. And who remembers the Campaign for Grumpiness where Grumpiness is Due? Given Mr Reid's track record, I don't think I'm flouting the membership rules of either of those campaigns. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
On Apr 14, 3:49 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote: > On 14 Apr, 03:24, "Bill Reid" <hormelf...@happyhealthy.net> wrote: > > > Angus <nos...@gmail.com> wrote in message > >news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > > > 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 |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
"Bill Reid" <hormelfree@happyhealthy.net> writes:
> Angus <nospam@gmail.com> wrote in message > news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... >> >> 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 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 diagnose what they are doing wrong. -- Ben. |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
On 14 Apr, 10:34, ymunt...@gmail.com wrote:
> On Apr 14, 3:49 am,Nick Keighley<nick_keighley_nos...@hotmail.com> > wrote: > > > > > > > On 14 Apr, 03:24, "Bill Reid" <hormelf...@happyhealthy.net> wrote: > > > > Angus <nos...@gmail.com> wrote in message > > >news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > > > > 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? is *what* the error I mentioned? The error above occurs on the line I indicated. > What does it warn about? 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)). 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 ful 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. > Parameter is declared as a function, that's adjusted to > the pointer to a function. What exactly is wrong? is it? -- Nick Keighley |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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@hotmail.com> wrote in message news:ef88a627-7556-4c17-a5ed-c1131d39ba50@m73g2000hsh.googlegroups.com... On 14 Apr, 10:34, ymunt...@gmail.com wrote: > On Apr 14, 3:49 am,Nick Keighley<nick_keighley_nos...@hotmail.com> > wrote: > > On 14 Apr, 03:24, "Bill Reid" <hormelf...@happyhealthy.net> wrote: > > > Angus <nos...@gmail.com> wrote in message > > >news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > > > > 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 Dick" AKA "troll zero"...remember, we're allegedly trying to "" a confused person here, but to do that, we have to "" 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 ful 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 |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
On Apr 14, 6:50 am, Nick Keighley <nick_keighley_nos...@hotmail.com> wrote: > On 14 Apr, 10:34, ymunt...@gmail.com wrote: > > > > > On Apr 14, 3:49 am,Nick Keighley<nick_keighley_nos...@hotmail.com> > > wrote: > > > > On 14 Apr, 03:24, "Bill Reid" <hormelf...@happyhealthy.net> wrote: > > > > > Angus <nos...@gmail.com> wrote in message > > > >news:fttgg7$g7k$1$8302bc10@news.demon.co.uk... > > > > > 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 ch |