CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Array size

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ln_mark7930



Joined: 06 Mar 2009
Posts: 21

View user's profile Send private message Send e-mail

Array size
PostPosted: Thu Oct 01, 2009 7:40 am     Reply with quote

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
};
Ttelmah
Guest







PostPosted: Thu Oct 01, 2009 9:19 am     Reply with quote

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
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Oct 01, 2009 11:40 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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