473,786 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structures with variable length array known at compile time

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.
Sep 14 '08
27 3299
aravind <ar*******@gmai l.comwrites:
On Sep 14, 9:45Â*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>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.
Sep 14 '08 #11
aravind <ar*******@gmai l.comwrites:
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.

Sep 15 '08 #12
On Sep 15, 2:33*am, aravind <aramos...@gmai l.comwrote:
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", "supercalifragi listic" };
const struct menu mainmenu =
{ mainmenu_items, dimof(mainmenu_ items), q };

Sep 15 '08 #13
On 14 Sep, 17:28, CBFalconer <cbfalco...@yah oo.comwrote:
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.embe dded might be a better place,
if it wasn't homework.
don't talk rubbish. The question is on-topic.

--
Nick Keighley
Sep 15 '08 #14
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.i nvalidwrote:
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] (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)
Sep 15 '08 #15
Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.i nvalidwrote:
<snip>
>>
...but by outlawing malloc, you make it very difficult!

it doesn't seem *that* 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.
};

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...
Yes.
menu_string could also be a 2D array.
Yes, but it doesn't fix the problem.
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.

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.
>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.
Feel free. :-)
>

--
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)
Gotta love Dan, haven't you? :-)

--
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
Sep 15 '08 #16
On 15 Sep, 10:17, Richard Heathfield <r...@see.sig.i nvalidwrote:
Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.i nvalidwrote:
...but by outlawing malloc, you make it very difficult!
it doesn't seem *that* difficult!
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] (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>

--
Nick Keighley

My god it's full of stars!
Dave Bowman, on seeing HAL's source code
Sep 15 '08 #17

"Bartc" <bc@freeuk.comw rote in message
news:wO******** ***********@tex t.news.virginme dia.com...
aravind wrote:
>Each menu can have different no. of menu items so i want to use only
as much memory as required for 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;
};
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.)

--
Bartc

Sep 15 '08 #18
Nick Keighley <ni************ ******@hotmail. comwrites:
On 15 Sep, 10:17, Richard Heathfield <r...@see.sig.i nvalidwrote:
>Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.i nvalidwrote:
>...but by outlawing malloc, you make it very difficult!
it doesn't seem *that* difficult!

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.

--
Ben.
Sep 15 '08 #19
Ben Bacarisse said:
Nick Keighley <ni************ ******@hotmail. comwrites:
>On 15 Sep, 10:17, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>...but by outlawing malloc, you make it very difficult!

it doesn't seem *that* difficult!

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.
Probably my fault. I only meant that, with malloc, it would have been easy
to provide a very efficient solution. I think that, without it, the
solution is clunkier, harder to code, and less efficient. Given the
probable origin of the question as homework, I am reluctant to provide the
source that might conceivably explain what I mean far better than I can
explain it in English.

--
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
Sep 15 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2487
by: Jim Hudon | last post by:
i need to create an array of a size determined by a non-const variable: int char sampleArray; why does the following not work, and what can i do: const int constArraySize = arraySize; int char sampleArray[ constArraySize; for both of the above, i get the same error message when i compile:
7
6063
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
1
1776
by: Galen Somerville | last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated. As you can see in the code below, my structures use fixed length strings and known array sizes. Consequently I can save to files as a large byte array. This is a series of Lectures where there is a capacity for 8 instructors with up to 8 lectures each. So a parameters file made from glctInstTable is 2,232 bytes. The 64 lectures, for the above, each consist of...
16
4830
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to keep track on the structures... But it's a bit confusing - my program won't compile and I don't know what to do about the warnings/error messages. c:\documents and settings\dell\Desktop\test\main.c(5) : warning
3
2231
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
12
3888
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
18
4064
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
44
5815
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
3
4911
by: jaime | last post by:
Hi all. The source code download bundle for "Beginning C: From Novice to Professional, Fourth Edition" (ISBN: 1590597354) (Horton/Apress) contains a C source file (program9_09.c) which contains several instances of the following type of idiom: /* Program 9.9 REVERSI An Othello type game */ const int SIZE = 6;
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.