Comparing null-terminated string with a non null-terminated string in C
The deserialization library (messagepack) I am using does not provide null-terminated strings. Instead, I get a pointer to the beginning of the string and a length. What is the fastest way to compare this string to a normal null-terminated string? The fastest way is strncmp() which limits the length to be compared. if (strncmp(sa, sb, length)==0) ... This assumes however that the length you use is the maximum length of the two strings. If the null terminated string could have a bigger length, you first have to compare the length. if(strncmp(sa,sb, length)==0 && strlen(sa)<=length) // sa being