...
In this example, the strlen()
function is used to limit the number of times the function string_loop()
will iterate. The conditional statement inside the loop is activated when the current character in the string is the null NULL terminator. However, because strlen()
returns the number of characters that precede the null NULL terminator, the conditional statement never evaluates true.
...
Removing the dead code depends on the intent of the programmer. Assuming the intent is to flag and process the last character before the null NULL terminator, the conditional is adjusted to correctly determine if the i
refers to the index of the last character before the null NULL terminator.
Code Block | ||
---|---|---|
| ||
int string_loop(char *str) { size_t i; size_t len = strlen(str); for (i=0; i < len; i++) { /* ... */ if (str[i+1] == '\0') /* This code is now reached */ } return 0; } |
...