Re: Some issue with pointers
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Tue, 01 Jul 2008 00:00:34 +0100
Marco Trapanese <marcotrapaneseNOSPAM@xxxxxxxxx> writes:
I'm writing code for an embedded system and I need to create a
navigable menu system. I'm going to use these structures:
typedef struct {
char *name[2];
int type;
int num_param;
void *ptr;
} menuitem_t;
typedef struct {
char *name[2];
int num_items;
menuitem_t *items;
} menu_t;
The latter defines each menu or submenu with a name, a number of items
and a pointer to the items structure.
The former defines each menu item with a name, a type (see below) the
number of parameters - if any - and finally a generic pointer (see
below again).
I can't see why you'd have two. I'd merge them like this:
typedef struct menu {
char *name[2];
int type;
int num_param;
union {
void *vp;
struct menu *items;
} ptr;
} menu_t;
It must be simpler to have only one type (in the C sense) for menu
items, surely?
In this way I can generate menus of any complexity and very
flexible. The void pointer should point to another menu_t struct if
the item leads to a submenu, to an array if it leads to a parameter
list. But it could also leads to a function if the selection of this
item needs an action to be executed.
Function pointers are "special" in that there is no guarantee in C
that you can convert a void * to function pointer (nor vice versa).
Your implementation may offer this as an extension, but if you use a
union, you can add a function pointer type and not have any
portability worries.
Actually I have two questions:
1) I can't cast correctly the void *ptr. Let's say I want to access to:
char *par[] = {"First", "Second"};
Let's say that ptr got set like this: 'ptr = par;'. ptr is then a
char ** in disguise so simply writing:
char **strings = ptr;
would let you access strings[0] and strings[1].
ot invoke:
void myfunc();
Please, may you help me to understand how to cast and then to use that
pointer in these cases?
It is easiest not to use a cast. Just assign ptr to a variable of the
right type and off you go.
However, since this requires an extension to standard C, you might
want to consider using a union containing an element for each pointer
type you need. This means you don't have to reply on converting a
void * to a function pointer and it keeps the code very clean (at the
expense of a messy union).
2) It would be nice if I can know the path the user is following. For
example:
mainmenu -> seconditem -> firstitem
where each of these entities are menu_t structs.
How would you implement such a dynamic path?
I'd use a stack, probably implemented as a linked list.
--
Ben.
.
- Follow-Ups:
- Re: Some issue with pointers
- From: Marco Trapanese
- Re: Some issue with pointers
- From: Marco Trapanese
- Re: Some issue with pointers
- Prev by Date: Re: search for files
- Next by Date: Re: Fast fmodf(..., 1.0f)
- Previous by thread: Re: search for files
- Next by thread: Re: Some issue with pointers
- Index(es):
Relevant Pages
|