467,911 Members | 1,356 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,911 developers. It's quick & easy.

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
  • viewed: 2942
Share:
27 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by John Dann | last post: by
1 post views Thread by Galen Somerville | last post: by
16 posts views Thread by Martin Joergensen | last post: by
18 posts views Thread by Pedro Pinto | last post: by
44 posts views Thread by svata | last post: by
3 posts views Thread by jaime | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.