View previous topic :: View next topic |
Author |
Message |
johnrportsmouth Guest
|
constant array of variable length strings |
Posted: Thu Apr 24, 2008 2:34 am |
|
|
keep getting compiler error:
require to be constant:
when I use the following:
char const variable_strings[2] [*] = {"string 1", "string 222"};
Wont recognise [*] ?
Would appreciate if anyone has had the same error. Tks |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 24, 2008 2:43 am |
|
|
What is your compiler version?.
This only started working reasonably well in about the mid 4.030 compilers.
Best Wishes |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Thu Apr 24, 2008 4:26 am |
|
|
Ttelmah wrote: | What is your compiler version?.
This only started working reasonably well in about the mid 4.030 compilers... |
Are you sure about that? This syntax does not even make sense to me. What I have seen is this:
Code: |
char const *variable_strings[2] = {"string 1", "string 222"};
|
where variable_strings is an array of pointers pointing to the various strings. What is the compiler supposed to do with [2][*] ? Does it search through the specified strings and find the longest one and pad them all out with zeroes to make them equal length so indexing arithmetic will work?
Robert Scott
Real-Time Specialties |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 24, 2008 5:04 am |
|
|
The CCS compiler added the ability to have an array of constant text strings that vary in length, without all the wasted storage locations, to pad the strings to a fixed length. The syntax is as shown in the original posting. What happens, is that the data is stored as one long sequence of data (with just NULL characters between), and a separate 'table' is generated, holding the start addresses of each string in this sequence. It _costs_ storage space, if you are only using a couple of strings as shown, and is slower to access, but does work.
If you use the 'search' facility in the manual, for 'const', the last article entitled 'Using Program memory for data', shows the syntax.
The most likely thing to stop the shown syntax working, is that the compiler is set to use ANSI mode, or an older CCS mode. This feature _only_ works if you are running in CCS4 mode (the default), and is disabled in these other modes.
Best Wishes |
|
|
johnrportsmouth Guest
|
pointers to string arrays |
Posted: Fri Apr 25, 2008 11:46 am |
|
|
Thanks for your help and assistance.
yes I have been using the following:
char const *string_array [] = {"string 1", "string 222"};
j = 2;
z = strlen(*string_array[j]);
fprintf (z);
//z does not hold the length of the string
//my problem is I don't quite understand pointers and how to handle them.
Regards
John |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Apr 28, 2008 2:00 am |
|
|
Arrays have a zero based index so j=2 will not work.
Try j=0 or j=1 |
|
|
|