473,406 Members | 2,259 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,406 software developers and data experts.

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....(again 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 1990
On 19 Jul, 22:18, "googlinggoog...@hotmail.com"
<googlinggoog...@hotmail.comwrote:
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.programming. 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....(again 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_processing(); /* 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....(again 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....(again 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))fptr2)(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
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? ...
8
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...
4
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...
8
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 -------------------------------------------------------------------------------- ...
11
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...
79
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...
4
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.