![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Ln_mark7930
Joined: 06 Mar 2009 Posts: 21
|
Array size |
Posted: Thu Oct 01, 2009 7:40 am |
|
|
Is it possible use array with variable length of data?
Now my code is:
Code: |
#define CARATTERI 20; // Numero di caratteri nella stringa dei menu
#define ELEMENTI 4 // Numero di elementi nel MENU
char const Menu[ELEMENTI] [CARATTERI] = {
"prova 1 ",//0
"prova 2 ",//1
"prova 3 ",//2
"prova 4 "3
};
|
Is it possible do this (array without fixed dimensions)? (to reduce space on program memory?):
Code: |
#define ELEMENTI 4 // Numero di elementi nel MENU
char const Menu[ELEMENTI] [CARATTERI] = {
"prova 1",//0
"Oggi c'รจ il sole e non piove ",//1
"prova prova prova",//2
"prova 4"//3
}; |
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Oct 01, 2009 9:19 am |
|
|
Yes. But obviously not with a length given.
From the manual:
const char colors[3] [*] = {"Red", "Green", "Blue"};
However, _caveat_, the resulting code is bulkier than using fixed length strings. It is not worth doing, if you are only saving a few bytes, since you lose more than you gain. The compiler has to store a separate table, containing the start locations of the strings, since it can no longer calculate them by simple multiplication....
Only actually 'works', on fairly recent compilers.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Oct 01, 2009 11:40 am |
|
|
One thing to remember when creating string variables, make sure you leave room for the terminating NULL character. If your string is 12 characters long your variable needs to be at least 13 bytes in size. Also, make sure you actually do terminate the string or you will get very erratic results down the road that will cause you to pull some hair out. I speak from experience, as I'm sure others can as well.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|