473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

callback pointing to a different function

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
3 1906

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
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
Thanks for your time. It's very clear and will help a lot.

ctitof

May 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric Entressangle | last post by:
Hi all, is there any trouble setting an C++ static class method as callback function for a C program or library ? Thanks
1
by: pvdm | last post by:
Hi, I am writing an app which encapsulates a multimedia timer. I implemented a TimerProc as static member function and a static member variable pThis as pseudo this variable to access in the...
5
by: Francois De Serres | last post by:
Hiho, could somebody please enlighten me about the mechanics of C callbacks to Python? My domain is more specifically callbacks from the win32 API, but I'm not sure that's where the problem...
8
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various...
3
by: PengYu.UT | last post by:
Hi, Would you tell me what callback function is used? Why we need it? I searched the web. But most webpages only mention that how to use it but not why to use it. Best wishes, Peng
8
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in...
2
by: Damien | last post by:
Hi all, I'm messing around with various signal/slot mechanisms, trying to build something lean and fast. I've used libsigc++, and Sarah Thompson's at sigslot.sourceforge.net, and most of the...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.