Suppose I want to make a function like
int indexOf ( char * str, char c )
{
// returns the index of the chracter c in the string str
// returns -1 if c is not a character of str
int k = 0;
while (*str)
{
if (*str == c) break;
else ++str;
}
return *str ? k : -1;
}
but I want to make it as reliable as possible. For instance, the above only works if the largest int
is guaranteed to be greater than or equal to the maximum size of a character array. How can I cover all my bases in pure C?