Structures with variable length array known at compile time

A

aravind

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.
 
B

Ben Bacarisse

aravind said:
Hi, I need to develop a menu system for a project of mine.
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.
 
C

CBFalconer

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 -

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.
 
R

Richard

CBFalconer said:
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.
 
B

Bartc

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
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;
};
 
A

aravind

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 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...
 
A

aravind

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;

};

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..
 
B

Bartc

aravind said:
#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.
 
B

Ben Bacarisse

aravind said:
aravind said:
Hi, I need to develop a menu system for a project of mine.
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)
}
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 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.

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.
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

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
};
 
T

Tim Rentsch

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
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.
 
O

Old Wolf

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?

I would make your menu structure contain a
pointer to a list of menu items. For example:

const char *mainmenu_items[] =
{ "item 1", "item number 2", "3", "supercalifragilistic" };


const struct menu mainmenu =
{ mainmenu_items, dimof(mainmenu_items), q };
 
N

Nick Keighley

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.

don't talk rubbish. The question is on-topic.
 
N

Nick Keighley

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!

it doesn't seem *that* difficult!

struct menu
{
int n (no. of elements in menu);
char menu_items[20][q];
char menu_items[n][20
};

const char* menu_string[] =
{"menu A", "menu B", "menu C"};

struct menu file_menu =
{2, {&menu_string[0], &menu_string[1]}, file_fp};

and since I didn't test this there are bound to be errors...

menu_string could also be a 2D array.


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 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.


and why is this bad?

This will waste some space (the malloc solution would be more
space-efficient), but not as much as you'd waste otherwise, I think.

I'd have thought you could get the memory efficeincy
with a "jagged" array.


--
Nick Keighley

The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);
(Dan Pop)
 
N

Nick Keighley

Nick Keighley said:

obviously it *should* have looked difficult...

struct menu
{
     int n (no. of elements in menu);

Perhaps you would care to put a value on n. The OP can't. He said that the
number varies, but is known at compilation time. That is, he has /several/
menus, with varying numbers of entries, but he appears to want a single
struct type that can cope with all of them, ...
      char menu_items[20][q];
        char menu_items[n][20

... so this won't do.

<snip embarrassing code>

ok, how about linked lists using a pool of
preallocated nodes

struct menu_item_node
{
struct menu_item_node *next;
char menu_item[21];
};

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
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.

and why is this bad?

It's not ghastly bad - it's just not as neat as dynamically allocating
exactly enough memory, and it will inevitably waste some space.


<snip>
 
B

Bartc

Bartc said:
aravind wrote:
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;
};

I think I was confused with another thread where someone wasn't allowed to
use pointers, but, yes, the .startindex field is best replaced by some
char** pointer to a dedicated char* array, one per menu, as suggested
elsewhere.

(Unless your compiler can only cope with a single char* array, then this
would be perfect.)
 
B

Ben Bacarisse

Nick Keighley said:
obviously it *should* have looked difficult...

Can someone explain what the problem is? The OP started off thinking
that char strings[n][20]; would do but we know it won't. One can
waste a little and use char (*strings)[20]; or one can just have char
**strings; and initialise that with a bunch of literals via a named
array (as posted by at least three people, I think).

It seems that I am missing why this is considered hard or a problem if
malloc is not allowed.
 
A

aramosfet

Thank you all for your replies and code snippets, i will try your
ideas when i start with coding today...
 
C

CBFalconer

Nick said:
don't talk rubbish. The question is on-topic.

Oh? You think ARM Micros and char LCD displays are on-topic?
Possibly they are never referenced again, but reading the post to
this point doesn't make that evident.
 
V

vippstar

Oh? You think ARM Micros and char LCD displays are on-topic?
Possibly they are never referenced again, but reading the post to
this point doesn't make that evident.

His question was on-topic. The background information was not.

Similar to:

I have many cats and I want an array to keep all their names.
I have this:

char **cat_names;

But I'm confused about using malloc with this... Can anyone help?

Cats are not on-topic. The question is.
 
K

Keith Thompson

CBFalconer said:
Oh? You think ARM Micros and char LCD displays are on-topic?
Possibly they are never referenced again, but reading the post to
this point doesn't make that evident.

No, it doesn't. Reading past that point does. If you don't have time
to read more than the first paragraph of a post, I'm surprised you
have time to post a followup claiming that it's off-topic.

aravind's was topical. He provided some background regarding the
motivation for what he's trying to do (something that too few posters
do). The fact that you didn't realize it was topical is your fault,
not his.

I don't recall seeing an off-topic post for which you, but nobody
else, pointed out that it was off-topic. I think you can safely take
a break. We've got it covered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top