Thursday, November 12, 2009

Be careful when you have to handle \0 character

I need to write some code to determine if a character belongs to a custom set. The code is as below:


bool isInMyCharSet(char ch)
{
const char* myCharSet="ABCD12349";
return strchr(myCharSet, ch)!=NULL?
}


The code works for years. Only one day you call this function with '\0'. Char 0 is not part of myCharSet, yet this function returns true.

Senior C programmers often know this, because this behavior is documented.


const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );

Locate first occurrence of character in string
Returns a pointer to the first occurrence of character in the C string str.

The terminating null-character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string.


OK. Fine, but do you know the same behavior exists in CString::Find method. This time, you won't find it mentioned in the document.

Now you may attempt to change the code to


for(int i=0; i if (myCharSet[i]==ch) return true;
}
return false;


Wrong again. sizeof operator still counts the terminating NUL character.