Re: XForms: fl_add_timeout

Steve Lamont (spl@szechuan.ucsd.edu)
Mon, 2 Nov 98 15:16:06 PST

# To subscribers of the xforms list from spl@szechuan.ucsd.edu (Steve Lamont) :

> I have a program that uses fl_add_timeout. The question I have is two fold.
> First, the function I am calling at timeout has some parameters that need
> to be passed to it, I'm not sure how to do this. Using ,
>
> fl_add_timeout(time, myfunc(param1, param2),NULL);
>
> doesn't work.

Unless myfunc() is a function returning pointer to function this no
not correct usage.

You should code

fl_add_timeout( time, myfunc, NULL );

When the timeout (time) expires, myfunc() will be called, with
parameters as dicussed in the manual.

> The second question I have is how to do the same thing using an array of
> pointers to functions. I have something like
>
> int (*myfuncs[8])();
>
> ....
>
> myfuncs[1] = somefunction();

Again, this is incorrect. This should be

myfuncs[1] = somefunction;

> How do I use fl_add_timeout with myfuncs[1], and again, I need to pass
> parameters.

fl_add_timeout( time, myfuncs[1], data );

The parameter `data' is a void pointer. Note, however, that this is
really a pointer mismatch, since the function pointer should be a
pointer to function returning void.

If you need to pass more than one argument, the best way is to create
a struct, fill it, and pass a pointer to the struct. For example:

struct my_struct {
int p1;
int p2;
int p3;
};

[...]

struct my_struct *my_struct_instance =
( struct my_struct *) malloc( sizeof( struct my_struct ) );

my_struct_instance->p1 = some_value;
my_struct_instance->p2 = some_other_value;
my_struct_instance->p3 = some_even_different_value;
fl_add_timeout( time, some_function, ( void *) my_struct_instance );

[...]

void some_function( int timeout_id, void *data )

{

struct my_struct *my_struct_instance = ( struct my_struct *) data;
int p1 = my_struct_instance->p1;
int p2 = my_struct_instance->p2;
int p3 = my_struct_instance->p3;

[...]

}

> I understand pointers, but have not used pointers to funtions before.

You may wish to refer to _The C Programming Language_ by Kernighan and
Ritchie for a rather lucid discussion of function pointers.

spl
_________________________________________________
To unsubscribe, send the message "unsubscribe" to
xforms-request@bob.usuf2.usuhs.mil or see
http://bob.usuf2.usuhs.mil/mailserv/xforms.html
XForms Home Page: http://bragg.phys.uwm.edu/xforms
List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/