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



"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:
<snip lots>

Why do you have to be so angry? There is a technical matter here so I
will get stuck in, but I am wary of getting caught in the middle of a
flame war.

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

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. Usually, the
function definition is similar to the declaration, except the ';' is
replaced by the function body. Here, you have an odd-looking
parameter. Function pointer parameters usually look like this:

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

which is, of course, how you wrote it in the function's prototype.
This example *seems* to declare the parameter as a function, not a
pointer to one.

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

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

This is the same sort of special case that makes 'int x[]' be treated
as a pointer rather than an (un-passable) array. Your example is less
than clear for people learning the language or having trouble with
function pointers. I would not post it as an example to follow, but
it is correct.

--
Ben.
.



Relevant Pages

  • Re: AnsiString variable on Stack even weirder ?!?!
    ... >>I was interesting in learning more about how delphi works with a real ... > Only the 4 byte pointer is on the Stack ...
    (alt.comp.lang.borland-delphi)
  • Re: Memory leak - What the ...?
    ... > One of the areas where Ada is absolutely counter-intuitive and ... expect anyone to know how Ada.Controlled works without learning it? ... Adjust is called). ... The assignment throws away the copy of the pointer, ...
    (comp.lang.ada)
  • Re: Is Python type safe?
    ... pointer arithmetic is certainly a different class of operations ... unrelated meaning than addition between strings and integers in other ... \__/ Liberty without learning is always in peril; ... liberty is always in vain. ...
    (comp.lang.python)
  • When to use pointers?
    ... Here's a probably quite stupid question but I'm still learning... ... it allows pass-by-reference function calls. ... large member object that you know you will never pass around as a parameter, ... make the above a pointer to the array instead of as here, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: including cray pointers in fortran modules
    ...  pointer (ipt, x) ... The first layer is with the declaration of ipt and that PROGRAM ... Cray pointer at has 4 bytes of precision; ...   ...
    (comp.lang.fortran)