By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

callback pointing to a different function

P: n/a
Hi all,
It's the first time I'm using groups so i hope I am posting on the good
group.

I''m not familiar with callback and function pointer and here is what
I'd like to do:

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
void *callback;
};

with callback a pointer to a function. Control will be used in 2 cases,
and depending on the case, it will call one function or the other. As
the arguments are not the same, how can I declare it? I was thinking of
doing the following:
typedef (* case1) (int arg1, int arg2, float arg3);
typedef (* case2 )(int arg1, float arg2, char arg3);

Control panel;
Control menu;
new_(panel, PANEL_TYPE)
new_(menu,MENU_TYPE)
where new_ initialize panel and menu, making the callback pointing to
the good function...

In one case, I would call (case1) (panel.callback)(int1,int2,float3);
in the other case, I would call (case2)(menu.callback)(int1, float2,
char3)

Is this could work, or I have not understood how it works :( . I've
been looking for a tutorial on callbacks, but I haven't found something
for my problem, where a callback could be assigned to 2 differents
functions with differents arguments.

Thanks for the help.
ctitof

May 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

christophe.ve...@gmail.com wrote:
Hi all,
It's the first time I'm using groups so i hope I am posting on the good
group.

I''m not familiar with callback and function pointer and here is what
I'd like to do:

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
void *callback;
};

with callback a pointer to a function. Control will be used in 2 cases,
and depending on the case, it will call one function or the other. As
the arguments are not the same, how can I declare it? I was thinking of
doing the following:
typedef (* case1) (int arg1, int arg2, float arg3);
typedef (* case2 )(int arg1, float arg2, char arg3);

Control panel;
Control menu;
new_(panel, PANEL_TYPE)
new_(menu,MENU_TYPE)
where new_ initialize panel and menu, making the callback pointing to
the good function...

In one case, I would call (case1) (panel.callback)(int1,int2,float3);
in the other case, I would call (case2)(menu.callback)(int1, float2,
char3)

Is this could work, or I have not understood how it works :( . I've
been looking for a tutorial on callbacks, but I haven't found something
for my problem, where a callback could be assigned to 2 differents
functions with differents arguments.

Thanks for the help.
ctitof


The argument lists would be the same if the logical arguments were
packed into a single structure, but different for each function, and
their actual argument was a void pointer which each function cast
to the appropriate type of struct pointer. But the possibilities of
mistakes in use are then greater.
--

May 2 '06 #2

P: n/a
ch**************@gmail.com wrote:
Hi all,
It's the first time I'm using groups so i hope I am posting on the good
group.

I''m not familiar with callback and function pointer and here is what
I'd like to do:

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
void *callback;
};

with callback a pointer to a function.
void* is not suitable for storing a function pointer.
Control will be used in 2 cases,
and depending on the case, it will call one function or the other. As
the arguments are not the same, how can I declare it? I was thinking of
doing the following:
typedef (* case1) (int arg1, int arg2, float arg3);
typedef (* case2 )(int arg1, float arg2, char arg3);


You have not specified what return type these functions have.

<snip>

Use one function pointer type in the struct, and cast it to the correct
function pointer type before calling it. E.g.

typedef void (dummy_func_type)(void)

typedef struct Control{
TYPE Type;
unsigned int x;
unsigned int y;
unsigned int width;
unsigned int height;
dummy_func_type *callback;
};

typedef void (case1_func_type)(int arg1, int arg2, float arg3);
typedef void (case2_func_type)(int arg1, float arg2, char arg3);

switch (foo.Type) {
case 1: ((case1_func_type*)foo.callback)(i1,i2,f);
break;
case 2: ((case2_func_type*)foo.callback)(i,f,c);
break;

and so on.

Note that I have defined a type for the function, not a pointer to a
function. I find this clearer.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 2 '06 #3

P: n/a
Thanks for your time. It's very clear and will help a lot.

ctitof

May 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.