Explorar el Código

Added missing null terminator (#1820)

Added missing null terminator when adding characters to the string, otherwise garbage values are read (often zeros which are equal to '\0', but not every time).

This error results in random characters appearing in the text box every one in a while:
```
asdfg??? ll??
```
It is corrected with the proposed fix.

This problem was observed by my student, Gonzalo Rivera Lazo.
pull/1821/head
Francisco Javier Andrés Casas Barrientos hace 4 años
cometido por GitHub
padre
commit
4dd5643402
No se encontró ninguna clave conocida en la base de datos para esta firma ID de clave GPG: 4AEE18F83AFDEB23
Se han modificado 1 ficheros con 2 adiciones y 1 borrados
  1. +2
    -1
      examples/text/text_input_box.c

+ 2
- 1
examples/text/text_input_box.c Ver fichero

@ -22,7 +22,7 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [text] example - input box"); InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for line ending char '\0'
char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for null terminator char '\0'
int letterCount = 0; int letterCount = 0;
Rectangle textBox = { screenWidth/2 - 100, 180, 225, 50 }; Rectangle textBox = { screenWidth/2 - 100, 180, 225, 50 };
@ -56,6 +56,7 @@ int main(void)
if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS)) if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
{ {
name[letterCount] = (char)key; name[letterCount] = (char)key;
name[letterCount+1] = '\0'; // Add null terminator at the end of the string.
letterCount++; letterCount++;
} }

Cargando…
Cancelar
Guardar