Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structures with variable length array known at compile time

Reply
Thread Tools

Structures with variable length array known at compile time

 
 
aravind
Guest
Posts: n/a
 
      09-14-2008
Hi, I need to develop a menu system for a project of mine. The menu
will be displayed on a character LCD display driven by ARM7
Microcontroller. For this purpose i wish to construct a structure in C
which will contain a the following -
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". 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?
Thanks.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-14-2008
aravind <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-14-2008
aravind wrote:
>
> Hi, I need to develop a menu system for a project of mine. The
> menu will be displayed on a character LCD display driven by ARM7
> Microcontroller. For this purpose i wish to construct a structure
> in C which will contain a the following -


Besides the fact that this appears to be homework, it is
off-topic. c.l.c deals with the C language, as defined in the
various C standards. comp.arch.embedded might be a better place,
if it wasn't homework.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      09-14-2008
CBFalconer <(E-Mail Removed)> writes:

> aravind wrote:
>>
>> Hi, I need to develop a menu system for a project of mine. The
>> menu will be displayed on a character LCD display driven by ARM7
>> Microcontroller. For this purpose i wish to construct a structure
>> in C which will contain a the following -

>
> Besides the fact that this appears to be homework, it is
> off-topic. c.l.c deals with the C language, as defined in the
> various C standards. comp.arch.embedded might be a better place,
> if it wasn't homework.


He is asking advice on how best to design a structure. The rest of the
information is merely "in addition" and might well lead responsible
senior programmers to guide him more thoroughly.

If you wish to be disruptive and unhelpful please at least wait for
people who do indeed post off topic.

His question was polite and on topic for the C programming language.

Please take the effort to read the posts you are so quick to denigrate.
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      09-14-2008

"aravind" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi, I need to develop a menu system for a project of mine. The menu
> will be displayed on a character LCD display driven by ARM7
> Microcontroller. For this purpose i wish to construct a structure in C
> which will contain a the following -
> 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". 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?
> Thanks.


Just use this:

#define maxrows 16 /* Or whatever */
#define columns 20 /* Or 21 if strings used */

struct menu {
int n; /* Menu rows in use */
char menu_items[maxrows][columns];
funcptr fptr;
};

--
Bartc

 
Reply With Quote
 
aravind
Guest
Posts: n/a
 
      09-14-2008
On Sep 14, 9:45*pm, Richard Heathfield <(E-Mail Removed)> wrote:
> aravind said:
>
> > Hi, I need to develop a menu system for a project of mine. The menu
> > will be displayed on a character LCD display driven by ARM7
> > Microcontroller. For this purpose i wish to construct a structure in C
> > which will contain a the following -
> > 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

>
> Then you'll want that the other way around. Conceptually:
>
> * * * *char menu_items[n][20];
>
> Unfortunately, this isn't legal C. But fear not - there'll be a way...
>
> > * * *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". 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).

>
> ...but by outlawing malloc, you make it very difficult!
>
> > My menu
> > items will be known at compile time. how do i implement this?

>
> Frankly, the right answer is "malloc". Failing that, the best I can think
> of is to have the string data in some humungous great static-qualified
> array, and replace char menu_items[n][20] with int menu_items[N] for some
> sufficiently large N, where menu_items[i] (i ranging from 0 to
> yourstruct.n - 1, of course) gives the index into the humungous great
> static-qualified array for the ith string in the display.
>
> This will waste some space (the malloc solution would be more
> space-efficient), but not as much as you'd waste otherwise, I think.
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999

The reason i dont want to use malloc is that the all the menu items/
elements are defined at compile time and will be stored like a lookup
table in the flash code memory on the microcontroller. So there's no
dynamic allocation. If i were to use malloc the space would allocated
from onchip RAM of which i have only 64KB. the flash has a more
generous 256KB.

I'm new to writing programs at this level of complexity with 70-100
different menu screens and associated functions, so i'm not sure
what's best approach to such a problem. How can we design software for
menu based applications. I'd be glad to hear your ideas. Thanks

>This looks like homework (usually called coursework here in the UK) so
>I am reluctant to post code.

No this is not homework/coursework, I'm working on a 5.1 audio power
amplifier system(DIY). The microcontroller will control the the
amplifier and provide remote controlled menu based interface for the
user. You can have look at what i'm working on here->
http://picasaweb.google.com/aramosfet

Thanks for you replies...
 
Reply With Quote
 
aravind
Guest
Posts: n/a
 
      09-14-2008
On Sep 14, 10:38*pm, "Bartc" <(E-Mail Removed)> wrote:
> "aravind" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> > Hi, I need to develop a menu system for a project of mine. The menu
> > will be displayed on a character LCD display driven by ARM7
> > Microcontroller. For this purpose i wish to construct a structure in C
> > which will contain a the following -
> > 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". 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?
> > Thanks.

>
> Just use this:
>
> #define maxrows 16 * */* Or whatever */
> #define columns 20 * */* Or 21 if strings used */
>
> struct menu {
> *int n; * * /* Menu rows in use */
> *char menu_items[maxrows][columns];
> *funcptr fptr;
>
> };
>
> --
> Bartc


if i were to declare an array of such const structs, each having a
different value of maxrows would the compiler allocate space for 16
rows as in typedef or would it optimize and allocate space only for
the rows which have been initialized.

Each menu can have different no. of menu items so i want to use only
as much memory as required for this..
--
aramosfet
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      09-14-2008
aravind wrote:
> On Sep 14, 10:38 pm, "Bartc" <(E-Mail Removed)> wrote:


>> #define maxrows 16 /* Or whatever */
>> #define columns 20 /* Or 21 if strings used */
>>
>> struct menu {
>> int n; /* Menu rows in use */
>> char menu_items[maxrows][columns];
>> funcptr fptr;


> if i were to declare an array of such const structs, each having a
> different value of maxrows would the compiler allocate space for 16
> rows as in typedef or would it optimize and allocate space only for
> the rows which have been initialized.
>
> Each menu can have different no. of menu items so i want to use only
> as much memory as required for this..


If you are short of memory and don't want to use malloc() then you need a
different approach.

Also, storing 20 characters per element would also be wasteful if elements
are shorter than that.

But perhaps try this:

char *menustrings[] = {
"menu1 row1", /* 0 Substitute actual text */
"menu1 row2", /* 1 */
"menu1 row3", /* 2 */
"menu2 row1", /* 3 */
"menu3 row1", /* 4 */
"menu3 row2"}; /* 5 */

struct menu {
int startindex; /* Start index of 1st elem in menustrings */
int n; /* Number of elements from menustrings */
funcptr fptr;
};

struct menu menulist[] = {
{0,3, 0}, /* Replace final 0 with funcptr value */
{3,1, 0},
{4,2, 0}};


int main(void){
puts(menustrings[menulist[2].startindex]); /* Show first elem of 3rd
menu */
}

This will minimise string storage. You can also use short int inside the
struct.

--
bartc

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-14-2008
aravind <(E-Mail Removed)> writes:

> On Sep 14, 9:45*pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> aravind said:
>>
>> > 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)
>> > }

<snip>
>> > My menu
>> > items will be known at compile time. how do i implement this?

>>
>> Frankly, the right answer is "malloc". Failing that, the best I can think
>> of is to have the string data in some humungous great static-qualified
>> array, and replace char menu_items[n][20] with int menu_items[N] for some
>> sufficiently large N, where menu_items[i] (i ranging from 0 to
>> yourstruct.n - 1, of course) gives the index into the humungous great
>> static-qualified array for the ith string in the display.

<snip>
> The reason i dont want to use malloc is that the all the menu items/
> elements are defined at compile time and will be stored like a lookup
> table in the flash code memory on the microcontroller. So there's no
> dynamic allocation.

<snip>
> I'm new to writing programs at this level of complexity with 70-100
> different menu screens and associated functions, so i'm not sure
> what's best approach to such a problem. How can we design software for
> menu based applications. I'd be glad to hear your ideas. Thanks
>
>>This looks like homework (usually called coursework here in the UK) so
>>I am reluctant to post code.

> No this is not homework/coursework, I'm working on a 5.1 audio power
> amplifier system(DIY). The microcontroller will control the the
> amplifier and provide remote controlled menu based interface for the
> user.


OK, I can make a suggestion with a clear conscience! I would do it
like this:

typedef void (*funcptr)(void);

struct menu {
int n;
char (*strings)[21];
funcptr fptr;
};

char menu1_strings[][21] = {
"abc",
"def",
"ghi"
};

void menu1_func(void)
{
/* ... as required */
}

struct menu menu1 = {
sizeof menu1_strings / sizeof menu1_strings[0],
menu1_strings,
menu1_func
};


The use of a pointer to an and array is odd but a reasonable strategy
here. Note that there is no need to waste the space a having 20
character strings -- this could be re-done like this:

struct menu {
int n;
const char **strings;
funcptr fptr;
};

const char *menu1_strings[] = {
"abc",
"def",
"ghi"
};

struct menu menu1 = {
sizeof menu1_strings / sizeof menu1_strings[0],
menu1_strings,
menu1_func
};

--
Ben.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      09-15-2008
aravind <(E-Mail Removed)> writes:

> Hi, I need to develop a menu system for a project of mine. The menu
> will be displayed on a character LCD display driven by ARM7
> Microcontroller. For this purpose i wish to construct a structure in C
> which will contain a the following -
> 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". 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?
> Thanks.


This case is one where a pointer to array might be a good choice:

struct menu {
char (*items)[20]; /* pointer to array of item strings */
int n; /* number of items */
funcptr f;
};

char menu_1_strings[][20] = {
"first item",
"second item",
"third item",
};

struct menu menu_1 = {
menu_1_strings,
sizeof menu_1_strings / sizeof *menu_1_strings,
menu_1_f
};


/* now use menu_1.items[k] to get the k'th item string in menu_1 */

For convenience you might want to put in a typedef for an item string
type, and maybe a #define so the various 'struct menu' definitions
are a little more succinct. I left everything explicit so it would
be more clear how everything fits together.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return an array of known length from a function (that takes in an arrayusing pointers TFS_PhDgirl C++ 0 03-03-2012 12:13 AM
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
Well Known JavaScript Libraries for Data Structures? Dejas Javascript 5 10-14-2007 10:11 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Known at compile-time JKop C++ 13 07-20-2004 04:24 PM



Advertisments