A
arnuld
This is the example from section 4.1 of K&R2,page 69 in my book:
int str_index( char s[], char p[] )
{
int i, j, k;
for( i = 0; s != '\0'; ++i )
{
for( k = 0, j = i; ( (p[k] != '\0') && (s[j] == p[k]) ); ++k, ++j )
;
if( (k > 0) && p[k] == '\0' )
{
return i;
}
}
return -1;
}
Since I feel lots of difficulty with code from K&R2, I alongside also read
Steve's K&R2 notes. From here:
http://www.eskimo.com/~scs/cclass/krnotes/sx7a.html
I see that Steve says the "k > 0" test in last "if" statement is not
essential. I removed that from K&R2's original example and the program is
running fine without any semantic-bug at all.
I want to know why K&R put thay "k > 0" there ? what's its importance ?
int str_index( char s[], char p[] )
{
int i, j, k;
for( i = 0; s != '\0'; ++i )
{
for( k = 0, j = i; ( (p[k] != '\0') && (s[j] == p[k]) ); ++k, ++j )
;
if( (k > 0) && p[k] == '\0' )
{
return i;
}
}
return -1;
}
Since I feel lots of difficulty with code from K&R2, I alongside also read
Steve's K&R2 notes. From here:
http://www.eskimo.com/~scs/cclass/krnotes/sx7a.html
I see that Steve says the "k > 0" test in last "if" statement is not
essential. I removed that from K&R2's original example and the program is
running fine without any semantic-bug at all.
I want to know why K&R put thay "k > 0" there ? what's its importance ?