Re: Structures with variable length array known at compile time
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sun, 14 Sep 2008 16:28:41 +0100
aravind <aramosfet@xxxxxxxxx> writes:
Hi, I need to develop a menu system for a project of mine.<snip>
struct menu
{
int n (no. of elements in menu);
char menu_items[20][q]; (This will contains the strings to be
displayed on the LCD, 20 characters and n lines
funcptr fptr; (Pointer to the corresponding menu function)
}
the array "menu_items" will always have 20 character strings but the
no. of them 'q' will differ from each menu screen. the no. of strings
will be defined in "int n".
This looks like homework (usually called coursework here in the UK) so
I am reluctant to post code. I will point out a couple of things...
You have the array the wrong what round -- what you wrote is an array
of 20 arrays of q characters each.
If the strings are to be 20 characters, it is better to use 21
character arrays and always have a null at the end. I.e. always keep
a proper string. If memory if very tight you can avoid this, but the
coding gets much more fiddly.
I will be using this struct to implement
const structs which i will be defining with all the menu screen
information.
From googling around i found variable length arrays cannot be
implemented with in structures. I also found that the structure size
should be known at compile time(i dont want to use malloc). My menu
items will be known at compile time. how do i implement this?
One way is to have a pointer to an array. These are a little odd, but
you have function pointers so array pointer are only a small step
away! Define the string array separately and then point to in when
you initialise the menu struct.
--
Ben.
.
- References:
- Prev by Date: Re: multi dimensional arrays as one dimension array
- Next by Date: Re: i donno the problem
- Previous by thread: Structures with variable length array known at compile time
- Next by thread: Re: Structures with variable length array known at compile time
- Index(es):
Relevant Pages
|