Passing "callback" function to a function 
Author Message
 Passing "callback" function to a function

Hello below is the usual simple/stupid example that should
self-eplain what I'm trying to accomplish.

Could you please help with that ?

Thank you.

#include <stdio.h>
void print_number ( int n )
{
  printf ("%d\n", n);

Quote:
}

void print_numbers (int n1, int n2,  /* !! (print_number_function_here) !! */  )
{
   for (i = n1; i <= n2; i++)
    /* !! print_number_function_here !! */( i );
Quote:
}

int main ( int argc, char *argv )
{
   print_numbers (0, 100,  /* ??? */ );
   return 0;
Quote:
}



Fri, 08 Jul 2005 01:22:39 GMT  
 Passing "callback" function to a function

Works fine with me, give or take that youactually insert the
function name as a parameter, and delare i in print_numbers):

#include <stdio.h>

void print_number ( int n )
{
  printf ("%d\n", n);

Quote:
}

void print_numbers (int n1, int n2, void (*func)(int)  )
{
  int i;
  for (i = n1; i <= n2; i++)
    func( i );

Quote:
}

int main ( int argc, char *argv )
{
  print_numbers (0, 100,  print_number );
  return 0;
Quote:
}



Fri, 08 Jul 2005 01:33:06 GMT  
 Passing "callback" function to a function

Quote:

> Hello below is the usual simple/stupid example that should
> self-eplain what I'm trying to accomplish.

> Could you please help with that ?

> Thank you.

> #include <stdio.h>
> void print_number ( int n )
> {
>   printf ("%d\n", n);
> }
> void print_numbers (int n1, int n2,  /* !! (print_number_function_here) !! */  )

void print_numbers(int n1, int n2, void (*print)(int n))

Quote:
> {
>    for (i = n1; i <= n2; i++)
>     /* !! print_number_function_here !! */( i );

        print(i);

        /* _or_ if you prefer to explicitly show that it's a pointer: */
        (*print)(i);

Quote:
> }
> int main ( int argc, char *argv )
> {
>    print_numbers (0, 100,  /* ??? */ );

        print_numbers(0, 100, &print_number);

Quote:
>    return 0;
> }

--

Stig
--
brautaset.org



Fri, 08 Jul 2005 01:30:52 GMT  
 Passing "callback" function to a function

Quote:

> Hello below is the usual simple/stupid example that should
> self-eplain what I'm trying to accomplish.

> Could you please help with that ?

#include <stdio.h>

void print_number (int n)
{
    printf ("%d\n", n);

Quote:
}

void print_numbers (int n1, int n2, void (*func_ptr) (int))
{
    int i;
    for (i = n1; i <= n2; i++)
        (*func_ptr) (i);

Quote:
}

int main (int argc, char *argv [])
{
    print_numbers (0, 100, &print_number);
    return 0;

Quote:
}

Martin


Fri, 08 Jul 2005 01:28:22 GMT  
 Passing "callback" function to a function

Quote:

> Hello below is the usual simple/stupid example that should
> self-eplain what I'm trying to accomplish.

> Could you please help with that ?

> Thank you.

> #include <stdio.h>
> void print_number ( int n )
> {
>   printf ("%d\n", n);
> }
> void print_numbers (int n1, int n2,  /* !! (print_number_function_here) !!
> */  )

void print_numbers(int n1, int n2, void (*printfunc)(int))

Quote:
> {
>    for (i = n1; i <= n2; i++)
>     /* !! print_number_function_here !! */( i );

     (*printfunc)(i);

Quote:
> }
> int main ( int argc, char *argv )
> {
>    print_numbers (0, 100,  /* ??? */ );

     print_numbers(0, 100, print_number);

Quote:
>    return 0;
> }

HTH. HAND.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 08 Jul 2005 01:33:31 GMT  
 Passing "callback" function to a function

Quote:

> Hello below is the usual simple/stupid example that should
> self-eplain what I'm trying to accomplish.

> Could you please help with that ?

> Thank you.

> #include <stdio.h>
> void print_number ( int n )
> {
>   printf ("%d\n", n);
> }
> void print_numbers (int n1, int n2,  /* !! (print_number_function_here) !! */  )

  void print_numbers (int n1, int n2, void (*pf)(int) )

Quote:
> {

int i;

Quote:
>    for (i = n1; i <= n2; i++)
>     /* !! print_number_function_here !! */( i );

        pf(i);
or
        (*pf)(i);

Quote:
> }
> int main ( int argc, char *argv )

int main ( int argc, char *argv[] )

Quote:
> {
>    print_numbers (0, 100,  /* ??? */ );

     print_numbers (0, 100,  print_number);
Quote:
>    return 0;
> }



Fri, 08 Jul 2005 01:41:01 GMT  
 Passing "callback" function to a function
First of all thank you very much for your neat replies !
Then, another question, are the funcion (the one passed as
pointer ) parameters always to be specified as you all did ?

eg:
#include <stdio.h>
void callback ( int foo, char *bar ) { }
void foobar   ( (void) *clb_func )
{
   (*clb_func) ( 10, "barfoo");

Quote:
}

int main ( int argc, char *argv )
{
   foobar ( &callback );
   return 0;

Quote:
}

?

Thanks again.



Fri, 08 Jul 2005 01:52:15 GMT  
 Passing "callback" function to a function

Quote:

> First of all thank you very much for your neat replies !
> Then, another question, are the funcion (the one passed as
> pointer ) parameters always to be specified as you all did ?

> eg:
> #include <stdio.h>
> void callback ( int foo, char *bar ) { }
> void foobar   ( (void) *clb_func )

This is wrong. The correct one is
void foobar ( void (*clb_func)(int, char*) )

Quote:
> {
>    (*clb_func) ( 10, "barfoo");
> }
> int main ( int argc, char *argv )
> {
>    foobar ( &callback );

The & operator is not necessary.
Quote:
>    return 0;
> }



Fri, 08 Jul 2005 02:14:17 GMT  
 Passing "callback" function to a function
"Nejat AYDIN"  wrote

Quote:

> > First of all thank you very much for your neat replies !
> > Then, another question, are the funcion (the one passed as
> > pointer ) parameters always to be specified as you all did ?

So from your reply I guess the answer to this question is:
"Yes they always must be specified".

 Thanks !

Quote:
> > eg:
> > #include <stdio.h>
> > void callback ( int foo, char *bar ) { }
> > void foobar   ( (void) *clb_func )

> This is wrong. The correct one is
> void foobar ( void (*clb_func)(int, char*) )



Fri, 08 Jul 2005 02:21:49 GMT  
 Passing "callback" function to a function

Quote:
> Hello below is the usual simple/stupid example that should
> self-eplain what I'm trying to accomplish.

I hope it's self-explaining too:

typedef void print_number_f (int n);

static void print_number (int n)
{
   printf ("%d\n", n);

Quote:
}

static void print_numbers (int n1, int n2, print_number_f *pf)
{
   for (i = n1; i <= n2; i++)
   {
      if (pf)
      {
         pf (i);
      }
   }

Quote:
}

int main (int argc, char *argv)
{
   print_numbers (0, 100, print_number);
   return 0;

Quote:
}

Looks very similar to a more detailed example I gave some days ago:


--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/manuals/reader.aspx
"Give peace a chance!"



Fri, 08 Jul 2005 02:28:52 GMT  
 Passing "callback" function to a function

Quote:
> int main (int argc, char *argv [])
> {
>     print_numbers (0, 100, &print_number);

No need for the '&'

Quote:
>     return 0;
> }

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/manuals/reader.aspx
"Give peace a chance!"


Fri, 08 Jul 2005 02:31:38 GMT  
 Passing "callback" function to a function

Quote:
> Then, another question, are the function (the one passed as
> pointer ) parameters always to be specified as you all did ?

It's better for consistency and compiler's checking.

Quote:
> eg:
> #include <stdio.h>
> void callback ( int foo, char *bar ) { }
> void foobar   ( (void) *clb_func )

This is a syntax error. For an easy maintenance, I strongly recommand the
help of a typedef:

Take the callback function prototype:

void callback ( int foo, char *bar );

Add a 'typedef';

typedef void callback ( int foo, char *bar );

give the type a _f (for function) suffix:

typedef void callback_f ( int foo, char *bar );

Done!

void foobar (callback_f *clb_func)

Much clearer, isn't it?

Quote:
> {
>    (*clb_func) ( 10, "barfoo");

stay simple:

   clb_func ( 10, "barfoo");

Quote:
> }
> int main ( int argc, char *argv )
> {
>    foobar ( &callback );

Note that the '&' is not required.

Quote:
>    return 0;
> }

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/manuals/reader.aspx
"Give peace a chance!"


Fri, 08 Jul 2005 02:38:11 GMT  
 Passing "callback" function to a function
"Emmanuel Delahaye" wrote
Quote:

> > Hello below is the usual simple/stupid example that should
> > self-eplain what I'm trying to accomplish.

> I hope it's self-explaining too:

[ snip]

Great, that is exactly what I wanted to do, thanks.



Fri, 08 Jul 2005 02:39:50 GMT  
 Passing "callback" function to a function

Quote:

> "Nejat AYDIN"  wrote

>> > First of all thank you very much for your neat replies ! Then,
>> > another question, are the funcion (the one passed as pointer )
>> > parameters always to be specified as you all did ?

> So from your reply I guess the answer to this question is: "Yes they
> always must be specified".

>  Thanks !

The cdecl program is useful when learning this stuff:

cdecl> declare function(int, int, pointer to function(int) returning void) returning void
void f(int , int , void (*)(int ))

--
A  program should be written to model the concepts of the task it
performs rather than the physical world or a process because this
maximizes  the  potential  for it to be applied to tasks that are
conceptually  similar and, more important, to tasks that have not
yet been conceived.



Fri, 08 Jul 2005 02:52:47 GMT  
 Passing "callback" function to a function

Quote:

> "Nejat AYDIN"  wrote

> > > First of all thank you very much for your neat replies !
> > > Then, another question, are the funcion (the one passed as
> > > pointer ) parameters always to be specified as you all did ?

> So from your reply I guess the answer to this question is:
> "Yes they always must be specified".

In fact, no! You can define a parameter of type pointer to
a function having unspecified number of parameters:
void foobar ( void (*clb_func)() )  { /* ... */ }
But if you call the function pointed by clb_func with wrong types or
numbers of arguments then the behaviour of the program will be undefined.
So it is always recommended to specify the parameters.
Quote:

> > > eg:
> > > #include <stdio.h>
> > > void callback ( int foo, char *bar ) { }
> > > void foobar   ( (void) *clb_func )

> > This is wrong. The correct one is
> > void foobar ( void (*clb_func)(int, char*) )



Fri, 08 Jul 2005 02:50:39 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. "Callback Function"

2. passing "function pointer"

3. Passing C++ Class Member Function to as a C Callback Function Parameter

4. Call to js function from VC++ returns "unknown name" ONLY in event sink callback function

5. How to call the function "RasEnumEntries"

6. C99 "remquo" function question

7. "scanf" function with assignment-suppression

8. Assigning "putchar" to a function pointer

9. what is the "static function"?

10. "extern" and function declarations

11. Searching for a "sounds-like" function

12. The mysterious "Call" function

 

 
Powered by phpBB® Forum Software