View previous topic :: View next topic |
Author |
Message |
M0KHZ
Joined: 14 Feb 2008 Posts: 18 Location: Cumbria
|
An array of Strings |
Posted: Wed Nov 12, 2008 11:41 am |
|
|
I've gone around in circles regarding this issue, and don't understand
why the code below doesn't compile.
compiler - Version 3.249
compiler error message:
Array of bits not permitted
Any ideas?
Code: |
// ------------------------------------------------------------------------
// Define string array 12 strings of length 20 characters
// ------------------------------------------------------------------------
char band_text[12][20];
// ------------------------------------------------------------------------
// Now we need to load the array with the text
// ------------------------------------------------------------------------
strcpy(&band_text[0][0],"Band = 160 Meters ");
strcpy(&band_text[1][0],"Band = 80 Meters ");
strcpy(&band_text[2][0],"Band = 40 Meters ");
strcpy(&band_text[3][0],"Band = 30 Meters ");
strcpy(&band_text[4][0],"Band = 20 Meters ");
strcpy(&band_text[5][0],"Band = 17 Meters ");
strcpy(&band_text[6][0],"Band = 15 Meters ");
strcpy(&band_text[7][0],"Band = 12 Meters ");
strcpy(&band_text[8][0],"Band = 10 Meters ");
strcpy(&band_text[9][0],"Band = 6 Meters ");
strcpy(&band_text[10][0],"Band = Spare 1 ");
strcpy(&band_text[11][0],"Band = Spare 2 ");
|
I'm planning to use this array like this:
Code: |
printf(LCD_PUTC,"%s",&band_text[band_pointer][0]);
|
Kevin - M0KHZ |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Wed Nov 12, 2008 12:05 pm |
|
|
Take out the & (address of) operator and the second [0] and you get a proper address.
Code: | strcpy(band_text[0],"Band = 160 Meters \0");
strcpy(band_text[1],"Band = 80 Meters \0");
strcpy(band_text[2],"Band = 40 Meters \0");
|
note: these are NOT strings, unless you didn't leave room for the \0 string terminator! |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Nov 13, 2008 3:08 am |
|
|
treitmey is basically correct but if you need strings of 20 char then create your array with string size of 21 to allow for the null terminating char.
char band_text[12][21];
// ------------------------------------------------------------------------
// Now we need to load the array with the text
// ------------------------------------------------------------------------
strcpy(band_text[0],"Band = 160 Meters ");
&band_text[0][0] and band_text[0] do the same thing so you should be able to use eather method. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Nov 13, 2008 8:55 am |
|
|
Ahh, I see. From the manual
Quote: | Copies a constant or RAM string to a RAM string. Strings are terminated with a 0. |
looks like it will put in the \0 itself. Just need to have room. |
|
|
M0KHZ
Joined: 14 Feb 2008 Posts: 18 Location: Cumbria
|
|
Posted: Fri Nov 14, 2008 2:07 pm |
|
|
Sorry for the delay in responding, but the day job has got in the way of fun!
Thanks for the answer guys. It's now working.
Kevin - M0KHZ |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Fri Nov 14, 2008 3:40 pm |
|
|
That was an incredibly misleading error message! Array of bits, indeed.
But you could save a lot of RAM space if you used shorter strings, and did the printing as:
printf(LCD_PUTC, "BAND = %s", band_text[0]);
where each string would be loaded like this:
strcpy(band_text[0], "160 Meters ");
and so forth. |
|
|
Guest
|
|
Posted: Fri Nov 14, 2008 4:32 pm |
|
|
Thanks John, this method is a whole lot smarter
Kevin - M0KHZ |
|
|
|