strncpy(messagetext,message,LCD_WIDTH); //good
messaetext[strlen(message)] //bad you need to check the length of message before possibly writing into crazy land
probably should look like:
#DEFINE LCD_LAST (LCD_WIDTH) -1
void
lcd_status(const char *message)
{
strncpy(messagetext, message, LCD_LAST); //leave room for the null
if (strlen(message) > LCD_LAST) {
messagetext[LCD_LAST] = 0; //message at max or too large
} else {
messagetext[strlen(message)] = 0;//message smaller than buffer
}
}
lcd_statuspgm can write past the end of messagetext if the message is too long....
coming out of the loop *target points to the fist byte beyond messagetext..
ran across some other stuff if you are interested..
在 lcd_status()
strncpy(messagetext,message,LCD_WIDTH); //good messaetext[strlen(message)] //bad you need to check the length of message before possibly writing into crazy land probably should look like: #DEFINE LCD_LAST (LCD_WIDTH) -1 void lcd_status(const char *message) { strncpy(messagetext, message, LCD_LAST); //leave room for the null if (strlen(message) > LCD_LAST) { messagetext[LCD_LAST] = 0; //message at max or too large } else { messagetext[strlen(message)] = 0;//message smaller than buffer } } lcd_statuspgm can write past the end of messagetext if the message is too long.... coming out of the loop *target points to the fist byte beyond messagetext.. ran across some other stuff if you are interested..