473,771 Members | 2,357 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 #1
27 3298
aravind <ar*******@gmai l.comwrites:
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.
Sep 14 '08 #2
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.embed ded 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.
Sep 14 '08 #3
CBFalconer <cb********@yah oo.comwrites:
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
Microcontrolle r. 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.embed ded 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.
Sep 14 '08 #4
CBFalconer said:
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
Microcontrolle r. 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.
No, it isn't. It is true that a small fraction (about 10%) of his article
gives unnecessary information about his platform, but this is irrelevant
to his question, which is entirely topical.

--
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 14 '08 #5
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
Sep 14 '08 #6

"aravind" <ar*******@gmai l.comwrote in message
news:f3******** *************** ***********@i24 g2000prf.google groups.com...
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

Sep 14 '08 #7
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. 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...
Sep 14 '08 #8
On Sep 14, 10:38*pm, "Bartc" <b...@freeuk.co mwrote:
"aravind" <aramos...@gmai l.comwrote in message

news:f3******** *************** ***********@i24 g2000prf.google groups.com...
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
Sep 14 '08 #9
aravind wrote:
On Sep 14, 10:38 pm, "Bartc" <b...@freeuk.co mwrote:
>#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(menustring s[menulist[2].startindex]); /* Show first elem of 3rd
menu */
}

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

--
bartc

Sep 14 '08 #10

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
1775
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
2230
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
3885
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
4063
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
5814
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
4909
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
9619
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
9454
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
10260
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
10038
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
9910
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
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4007
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
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.