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




Ben Bacarisse <ben.usenet@xxxxxxxxx> wrote in message
news:87iqykasl5.fsf@xxxxxxxxxxxx
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:
Ben Bacarisse <ben.usenet@xxxxxxxxx> wrote in message
news:87bq4cci3r.fsf@xxxxxxxxxxxx
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:
<snip lots>

<snip>
extern void my_library_function (int (*)(const char*));

library_c.c

#include "library_h.h"

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

You must be aware that is this not the normal syntax.

Sure it is. I use it all the time, so it's normal to me.

That's fine, but I would not suggest it as a "first example" or in
"how to" code just because it needs more explanation.

OK, that's sort of fair; as I've said, I'm all about helping
the "newbies" (won't SOMEBODY think of the children!??!!!)..

Usually, the
function definition is similar to the declaration, except the ';' is
replaced by the function body.

Right. Let me clue you in on something about me: I try to
conserve keystrokes where they AREN'T needed, and use
them where they help me understand my own code.

Again, that is up to you, but in my editor copying the prototype is
fewer keystrokes than writing the two forms you used.

Uh-oh, I knew it would come to this...time and motion on
key-presses...now I'm in REAL trouble...preparing to back-pedal...

<snip>
Function pointer parameters usually look like this:

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

Sure. But then I have to type "(*)" (three whole extra characters!)
where I don't need to type any more characters, because the two
ways of defining the parameters ARE TOTALLY FUNCTIONALLY
IDENTICAL.

which is, of course, how you wrote it in the function's prototype.

There, I'm forced to write it that way, unless I again want to
type unwanted characters...WHICH I DON'T.

This I don't get. You can save two keystrokes if you use the same
style in the prototype (but let me guess: a parameter name in a
prototype is "unwanted characters").

Well, yeah...also, as you might guess, we ALL have our
little peccadillos, and I HATE parameter names in prototypes,
and I'm sure I could find a compiler or LINT someplace that
would issue a warning agreeing with my childhood toilet-training
trauma, the same way the "troll-ette" found HIS compiler
that unearthed MY "error"...

<snip>
The answer is that there is a special dispensation:

A declaration of a parameter as ''function returning type'' shall be
adjusted to ''pointer to function returning type''

Great, which of course completely conforms to the basic
idea above that a function name is actually a function pointer
by the simple necessity of computer science...I'm assuming
this is "standard" language, so despite you calling it a
"special dispensation" IT IS TOTALLY CONFORMING
"STANDARD" SYNTAX.

Yes, but that does not make it good syntax. With your example, a
beginner would wonder why a variable declared to store this callback
pointer looked so different to the parameter that receives it. C has
enough special cases already.

Again, I don't even consider, or was aware, it a "special case", but
rather just another manifestation of the general case regarding function
pointers...

I'm sure the OP has run away in terror by now, but PART of his
problem seemed to be not understanding that basic concept...and
there was no post except mine that demonstrated it (actually, if
you read his post, he was REALLY confused about the whole
concept of "callbacks", and NOBODY posted anything coherent
to clear ANY of that confusion EXCEPT me)...

I don't want to have a style war about how you write your programs,
but in "how to" postings I would suggest being consistent and avoiding
special cases. At least some of the "fuzz" here may have come from
your use of two different styles.

Nah, the real problem is that it's always easier to be a critic than
an artist...like I say, programming I'm not that good at, it's PEOPLE
that I really know "inside-out"...

---
William Ernest Reid



.



Relevant Pages

  • Re: How do I create a function in my library for passing user callback function
    ... <snip lots> ... You must be aware that is this not the normal syntax. ... Again, that is up to you, but in my editor copying the prototype is ...
    (comp.lang.c)
  • Re: RosAsm - right click
    ... require to take a look at RosAsm itself as a Prototype, ... for several interresting aspects, and at NASM Syntax, ... was to completely exclude any concept of "Reserved Word". ... exceptional that they would not recompile without any problem ...
    (alt.lang.asm)
  • Re: the usage of void argument
    ... a function can be declared or defined using a prototype ... ** Function definition using old style, non-prototype syntax (it's been ... int f ... ** Function declaration using non-prototype syntax ...
    (comp.lang.c)
  • Re: What does this mean??
    ... It takes fewer keystrokes. ... Keep your eye out for the places Sun uses ... In the IF syntax you would write that perhaps as: ... The biggest problem with it is newbies can't read it. ...
    (comp.lang.java.programmer)
  • Re: get the address of a function??
    ... It's in violation of something, but it ain't syntax, and it's nothing to ... do with the fact that it's a prototype. ... "The implementation declares no prototype for this function." ...
    (comp.lang.c)