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 support@ccsinfo.com

Two dimensional arrays for strings

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



Joined: 23 Apr 2005
Posts: 16

View user's profile Send private message

Two dimensional arrays for strings
PostPosted: Mon Dec 11, 2006 9:59 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Dec 11, 2006 10:20 pm     Reply with quote

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







PostPosted: Tue Dec 12, 2006 3:16 am     Reply with quote

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
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