...
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; for (i=0; i < strlen(str); i++) { /* ... */ if (str[i+1] == '\0') /* This code is now reached */ } return 0; } |
...