473,797 Members | 3,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help structuring my program (arrays of function pointers/ passing variables to functions)

Hi

This should all be pretty standard C stuff, but I'm going to use terms
like mouse callback to communicate what Im tyring to do.

Basically I have my program whirling around in an infinite loop (easy)
waiting for the mouse to be clicked (I dont need help with this) and
depending on user input a variable might define what function I want
(be it line, circle....(agai n these are my issues)

So I was thinking that I could use something like an array of function
pointers because I can make all the functions exist with the same
function type I suppose, although that could potentially get messy

What I dont know how to do neatly (I could bodge something but im
trying to do this properly) is essentially have something as follows

1) inside loop some case statement where I can set some varible to
state i want to call the line, circle, etc (theres loads of these)
function. (all these functions in some array of function prototypes
somewhere?)

2)pass the number of mouse clicks to wait to some global variable that
can be picked up when I hit the mouse.

3) in same said mouse call back, use the variable set in point 1 to
get some function pointer someplace from some array of function
pointers and then call something like function_circle (int x, int y,
char color, char text) based on what i've set previously...

I can see lots of holes in this and was woundering whether someone
could help me with my ideas?

Regards

David

Jul 19 '07 #1
3 2019
On 19 Jul, 22:18, "googlinggoog.. .@hotmail.com"
<googlinggoog.. .@hotmail.comwr ote:
This should all be pretty standard C stuff, but I'm going to use terms
like mouse callback to communicate what Im tyring to do.
I think that's where your problem originates.
Yes you have a C program design problem. But you also have
a GUI implementation problem and maybe a UI design problem.

For general programming try comp.programmin g. For UI design-
I dunno.

Are you designing all this from scratch (brave man!) or are you using
an existing GUI framework? If the 2nd then there's probably an
existing
framework.
Basically I have my program whirling around in an infinite loop (easy)
waiting for the mouse to be clicked (I dont need help with this) and
depending on user input a variable might define what function I want
(be it line, circle....(agai n these are my issues)

So I was thinking that I could use something like an array of function
pointers because I can make all the functions exist with the same
function type I suppose, although that could potentially get messy
could do...

maybe:-

for (;;)
{
if (mouse_clicked)
{
if (using_tool)
draw[tool](mouse_posn); /* array of func ptr */
else
do_button_proce ssing(); /* may select new tool */
}
}

I think you need to decide how your GUI is going to work.
What I dont know how to do neatly (I could bodge something but im
trying to do this properly) is essentially have something as follows

1) inside loop some case statement where I can set some varible to
state i want to call the line, circle, etc (theres loads of these)
function. (all these functions in some array of function prototypes
somewhere?)

2)pass the number of mouse clicks to wait to some global variable that
can be picked up when I hit the mouse.

3) in same said mouse call back, use the variable set in point 1 to
get some function pointer someplace from some array of function
pointers and then call something like function_circle (int x, int y,
char color, char text) based on what i've set previously...

I can see lots of holes in this and was woundering whether someone
could help me with my ideas?

--
Nick Keighley
Jul 20 '07 #2
go************* @hotmail.com wrote:
Hi

This should all be pretty standard C stuff, but I'm going to
use terms like mouse callback to communicate what Im tyring to
do.

Basically I have my program whirling around in an infinite loop
(easy) waiting for the mouse to be clicked (I dont need help
with this) and depending on user input a variable might define
what function I want (be it line, circle....(agai n these are my
issues)

So I was thinking that I could use something like an array of
function pointers because I can make all the functions exist
with the same function type I suppose, although that could
potentially get messy
Don't force different function pointer types to one type. It'll
most likely lead to undefined behaviour.
What I dont know how to do neatly (I could bodge something but
im trying to do this properly) is essentially have something as
follows

1) inside loop some case statement where I can set some varible
to state i want to call the line, circle, etc (theres loads of
these) function. (all these functions in some array of function
prototypes somewhere?)
Array of function prototypes? There's no such thing. If you mean
an array of function pointers, then I suppose all the functions
have the same return type and parameter list?
2)pass the number of mouse clicks to wait to some global
variable that can be picked up when I hit the mouse.

3) in same said mouse call back, use the variable set in point
1 to get some function pointer someplace from some array of
function pointers and then call something like
function_circle (int x, int y, char color, char text) based on
what i've set previously...
Basically, I think we can ignore the mouse issue. To be brief,
your program waits for some input, (what is the type and format
of this input?), then picks a particular function pointer out of
an array of function pointers and deferences it.

I think the input receiving part, and function pointer retrieval
part should be in separate functions. The function pointer array
itself could be a global array. A switch statement could could
decide on the array subscript, or, if the choices are more
complex, an else-if construct could be used.
I can see lots of holes in this and was woundering whether
someone could help me with my ideas?
Without knowing more details about the structure and function of
your program, we cant give anything other than the most general
advice.
Jul 20 '07 #3
santosh wrote:
go************* @hotmail.com wrote:
>Hi

This should all be pretty standard C stuff, but I'm going to
use terms like mouse callback to communicate what Im tyring to
do.

Basically I have my program whirling around in an infinite loop
(easy) waiting for the mouse to be clicked (I dont need help
with this) and depending on user input a variable might define
what function I want (be it line, circle....(agai n these are my
issues)

So I was thinking that I could use something like an array of
function pointers because I can make all the functions exist
with the same function type I suppose, although that could
potentially get messy

Don't force different function pointer types to one type. It'll
most likely lead to undefined behaviour.
Just to be clear: santosh is thinking about the likelihood
of making mistakes later on, not about the conversion of the
function pointer itself. Any function pointer type can be
converted to any other function pointer type and back again
without damage; this is an explicit guarantee of the language.
However, it *is* undefined behavior if you call a function via
a pointer whose type doesn't match the function's actual type:

#include <math.h>
double (*fptr1)(double ) = sqrt; /* okay */
int (*fptr2)(int) = (int (*)(int))sqrt; /* okay */
...
double x = fptr1(2); /* okay */
double y = fptr2(2); /* undefined behavior */

By using appropriate casts, you can fill your array with
pointers to any functions of any type at all, but you *must*
convert the pointer back to the proper type when calling:

y = ((double (*)(double))fpt r2)(2); /* okay */

(A few typedefs can improve the readability quite a lot.)
Fail to convert when needed, or convert to the wrong type,
and you're toast.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 20 '07 #4

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

Similar topics

31
14351
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
8
2069
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to make the ESC key quit the called function when ever the user inputs data. The description was : ESC drops back to the main menu in case the user gets locked up or wants to start over again, ESC should provide a fool proof way to exit back to the...
4
1846
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
8
5483
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
11
2416
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to follow? Second, have I correctly passed the structure's pointer to the functions in this program?
79
3438
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same! 9: More about Arrays 10: More about Pointers What blows me out of the water is the fact that 'every' programmer
4
2506
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
11
3367
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
9537
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10209
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2934
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.