|
|
View previous topic :: View next topic |
Author |
Message |
csanders
Joined: 23 Apr 2005 Posts: 16
|
Two dimensional arrays for strings |
Posted: Mon Dec 11, 2006 9:59 pm |
|
|
I am trying to create an error log in memory using a 2 dimensional array to store a verbose message, but cannot get the code to compile.
Code: |
char errorlog[10][20];
int8 index;
//on error, place error message in array
if (index < 18)
{
errorlog[index++][0] = "error msg"; //compilation error occurs here
}
|
Does anyone know what I am doing wrong?
Thanks |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Dec 11, 2006 10:20 pm |
|
|
This is what I do. Perhaps it is that I use const char??
replace STDERR with any stream you've defined.
Code: | const char error_txt[16][16] =
{
"USART overflow", // -0-
"TXBuffer ovrflw", // -1-
"RXBuffer ovrflw", // -2-
"Bad Checksum", // -3-
"LCD busy", // -4-
"5", // -5-
"6", // -6-
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"NO ERRORS"
};
//somewhere else in code
bit_set(ERRORS,1);//set error flag bit 1 for tx overflow
unsigned int16 ERRORS;
void ChkErr(void) //print out any errors that occured
{
int8 chk=0;
if (ERRORS==0){return;} //if no errors jump right back out
for(chk=0;chk<16;chk++)
{
if (bit_test(ERRORS,chk))
{
bit_clear(ERRORS,chk);
fprintf(STDERR,"ERROR(%U): %s.\r\n",chk,error_txt[chk]);
tx_buffer(NACK,sizeof(NACK));
rx_indx_i=rx_indx_o=0;
}
}
} |
|
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 12, 2006 3:16 am |
|
|
To load data from a 'constant' string, into the array, you need to use 'memcpy' or 'strcpy'. So:
Code: |
strcpy(&errorlog[index++][0],"error msg");
|
You can't just 'hand' a string into the array.
As a separate comment, what is going to happen when index is greater than 9?. You are trapping it at 18, but the array dimension on this axis, only goes 0..9.
Best Wishes |
|
|
|
|
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
|