char array make lcd show garbage

I have a char array looks like this char Str[16] = {' ', ' ', ' ', ' ', ' ', ' ', '-', '*', '*', '*', ' ', ' ', ' ', ' ', ' ', ' '}; as pass but when i reassign part of it then print it again on lcd like that:

    Str[6] = "-";
    Str[7] = "*";
    Str[8] = "*";
    Str[9] = "*";
    Str[10] = " ";

  lcd.backlight();

    
   lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(F("PASSWORD"));
    lcd.setCursor(0, 1);
  lcd.print(Str);
    delay(1500);
    lcd.noBacklight();

for some reason some garbage chars show at the beginning of the lcd before the password. found that the assignment part or the lcd.print(Str); if they both or any is there it cause the garbage to show and but if they aren't they don't show. so any idea?

Where's the null string terminator?

When you pass a string to print, it expects to find a null terminator at the end to tell it where the end is. Your array doesn't have one.

so i need to add a null term at the end of the array?

    Str[6] = "-";
    Str[7] = "*";
    Str[8] = "*";
    Str[9] = "*";
    Str[10] = " ";

This would not compile because Str[6] is a character and "-" is a string.

It should be

   Str[6] = '-';
    Str[7] = '*';
    Str[8] = '*';
    Str[9] = '*';
    Str[10] = ' ';

and for good measure you should add a null on the end of the array. To do that you have to extend the array by one character 'char Str[17]' and then

 Str[16] = 0;

Pete

el_supremo:

    Str[6] = "-";

Str[7] = “";
    Str[8] = "
”;
    Str[9] = “*”;
    Str[10] = " ";



This would not compile because Str[6] is a character and "-" is a string.

It should be


Str[6] = ‘-’;
    Str[7] = ‘’;
    Str[8] = '
’;
    Str[9] = ‘*’;
    Str[10] = ’ ';




and for good measure you should add a null on the end of the array. To do that you have to extend the array by one character 'char Str[17]' and then


Str[16] = 0;




Pete

Shall i put the null terminator at the 17 or 16? I guess you meant 17?

A sixteen element array has neither index 16 nor index 17.

Got it.

@AWOL, el_supremo, aarg

Thanks a lot for the help

Max