473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 3256
aravind <ar*******@gmail.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.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.
Sep 14 '08 #3
CBFalconer <cb********@yahoo.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
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.
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
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.
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*******@gmail.comwrote in message
news:f3**********************************@i24g2000 prf.googlegroups.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.invalidwrote:
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.comwrote:
"aravind" <aramos...@gmail.comwrote in message

news:f3**********************************@i24g2000 prf.googlegroups.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.comwrote:
>#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

Sep 14 '08 #10
aravind <ar*******@gmail.comwrites:
On Sep 14, 9:45¬*pm, Richard Heathfield <r...@see.sig.invalidwrote:
>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*******@gmail.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...@gmail.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", "supercalifragilistic" };
const struct menu mainmenu =
{ mainmenu_items, dimof(mainmenu_items), q };

Sep 15 '08 #13
On 14 Sep, 17:28, CBFalconer <cbfalco...@yahoo.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.embedded 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.invalidwrote:
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.invalidwrote:
<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.invalidwrote:
Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.invalidwrote:
...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.comwrote in message
news:wO*******************@text.news.virginmedia.c om...
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.invalidwrote:
>Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.invalidwrote:
>...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.invalidwrote:
>>Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.invalidwrote:
>>...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
On Mon, 15 Sep 2008 13:49:07 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote:
>Ben Bacarisse said:
>Nick Keighley <ni******************@hotmail.comwrites:
>>On 15 Sep, 10:17, Richard Heathfield <r...@see.sig.invalidwrote:
Nick Keighley said:
On 14 Sep, 17:45, Richard Heathfield <r...@see.sig.invalidwrote:

...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.
The OP has already explained that it is not homework, and has
(briefly) explained the context and why he doesn't want to use
malloc. Since you seem to have missed it, here is the relevant
passage:

"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."
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Sep 15 '08 #21
Thank you all for your replies and code snippets, i will try your
ideas when i start with coding today...
--
aramosfet
Sep 15 '08 #22
Nick Keighley wrote:
CBFalconer <cbfalco...@yahoo.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.embedded might be a better place,
if it wasn't homework.

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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 15 '08 #23
On Sep 15, 11:07 pm, CBFalconer <cbfalco...@yahoo.comwrote:
Nick Keighley wrote:
CBFalconer <cbfalco...@yahoo.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.embedded might be a better place,
if it wasn't homework.
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.
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.
Sep 15 '08 #24
CBFalconer <cb********@yahoo.comwrites:
Nick Keighley wrote:
>CBFalconer <cbfalco...@yahoo.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.embedded might be a better place,
if it wasn't homework.

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '08 #25
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgsensibly chided our good friend CBF thusly:
....
>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.
Well done, sir! I see you and Richard (*) are finally on the same side
of an issue. That puts you on the right side for a change.

(*) He whom you often refer to as "he who has no last name" (or some
such piffle)

Sep 15 '08 #26
CBFalconer said:
Nick Keighley wrote:
>CBFalconer <cbfalco...@yahoo.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.embedded might be a better place,
if it wasn't homework.

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

Oh? You think ARM Micros and char LCD displays are on-topic?
No, but they are merely incidental - the question is on-topic.

If you must play topic-cop, please play good cop, not dumb cop.
Possibly they are never referenced again, but reading the post to
this point doesn't make that evident.
Try reading the *whole* article! It's only 21 lines, for Cthulhu's sake!

--
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 #27
On Sep 16, 1:49*am, Richard Heathfield <r...@see.sig.invalidwrote:
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.
I think it's is clunkier, harder to code, and
less efficient with malloc!

(Assuming that all of the table entries are
known at compile time, as the OP said).

Sep 15 '08 #28

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

Similar topics

2
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...
7
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...
1
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...
16
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...
3
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
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...
18
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
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
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.