The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Suggestion for a menu programming structure
Discuss Suggestion for a menu programming structure in the C Programming forum on Dev Shed. Suggestion for a menu programming structure C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 5th, 2012, 04:32 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Suggestion for a menu programming structure
I want to design a generic menu structure for easy management etc.
I have something in mind like this:
1- I plan to define a structure that fits into all menu item object types. It will contain the required values and functions. Then when i need a new menu item i'll allocate memory for it, set its parameters and add it to a dynamic list(like linked list). Then the menu engine will use this object.
example structure:
typedef struct MENU_ITEMS
{
unsigned int ID; // ID
int value; // Some value
void(*Draw)(void *); // Generic draw function
}MENU;
But there's a problem. Every menu item is not the same so they may have different parameters and most importantly they may have different draw functions with different number of paramaters and types. I want suggestions. How can I create such a structure for this kind of application that is flexible enough to expand easily and easily be managed. Thanks, i'll appreciate your ideas...
|

November 5th, 2012, 04:53 AM
|
 |
Contributed User
|
|
|
|
How about
Code:
typedef struct MENU_ITEMS
{
unsigned int ID; // ID
int value; // Some value
void *drawData; // pointer to draw-specific data
void(*Draw)(void *); // Generic draw function
}MENU;
So you call
foo->Draw(foo->drawData);
|

November 5th, 2012, 08:44 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem How about
Code:
typedef struct MENU_ITEMS
{
unsigned int ID; // ID
int value; // Some value
void *drawData; // pointer to draw-specific data
void(*Draw)(void *); // Generic draw function
}MENU;
So you call
foo->Draw(foo->drawData); |
So you say; define function parameters as another member of the struct and it may point to another struct where the draw function has different number and type of structs so you get the flexibility you want. Thanks, this may be a good idea. Any other suggestions? Especially for this kind of applications don't stick with my idea. May be there's a whole different approach which is more elegant?
|

November 5th, 2012, 02:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
The problem with C is strong typing and exact declarations, though that is also its blessing. But it does get in the way of some things we might like to do, like have a function with variable numbers of arguments.
What salem suggested is a simple and elegant solution that is used in multithreading, such that a standard thread function format can be declared that can handle any number and type of arguments via the void pointer.
There are also variable length argument lists (as they're called in the index in K&R's The C Programming Language (2nd ed.). They're declared in stdarg.h and use a va_list datatype and functions va_list, va_end, and va_arg. A page that describes how to use them (assuming you don't have K&R's book) is at http://www.techpulp.com/blog/2008/1...arguments-in-c/, though I'm sure with the keywords I just gave you you could Google for lots more examples and explanations.
I've never used va_arg's myself (learned about them in C class 23 years ago and never looked at them again until just now) and, from what I see, the approach borrowed from multithreading looks a lot simpler, cleaner, and more elegant.
|

November 5th, 2012, 02:25 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by dwise1_aol
I've never used va_arg's myself (learned about them in C class 23 years ago and never looked at them again until just now) and, from what I see, the approach borrowed from multithreading looks a lot simpler, cleaner, and more elegant. |
Hmm i've never paid attention to the argument list although i've seen it. It's an interesting idea, like overloading in c++ but as you suggested its usage seems a bit cumbersome. If we use this method(va_arg) how should we declare it in the structure?
|

November 5th, 2012, 02:59 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
I have no idea, having never played with that method. I recommend that you Google on the method and some of the examples may give you an idea of how to go about it.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|