I'm currently working on a string metric library, which calculates various distances between strings and reports how similar the strings are to one another. For example the Levenshtein Distance (https://en.wikipedia.org/wiki/Levenshtein_distance).
unsigned levenshtien(const char *str1, const char *str2)
{
// check for NULL pointers
if (str1 == NULL && str2 == NULL)
return 0;
if (str1 != NULL && str2 == NULL)
return strlen(str1);
if (str1 == NULL && str2 != NULL)
return strlen(str2);
// calculate length of strings
size_t str1_len = strlen(str1);
size_t str2_len = strlen(str2);
// handle cases where one or both strings are empty
if (str1_len == 0)
return (str2_len == 0) ? 0 : 1;
// calculate stuff here...
}
Each function in the library is passed const char *
pointers. I was wondering if it was common practice to check if each of the pointers was NULL
? Or should I just assume the programmer using the library would check before passing the pointers?