c - Comparing non null-terminated char arrays -


i have compressed unsigned char array of known size. due compression considiration don't store null-terminator @ end. want compare array of same format. best way so?

i thought duplicating comparessed arrays new array, add null-terminator, , compare them using strcmp().

any better suggestions?

you can use strncmp() function string.h

strncmp(str1, str2, size_of_your_string);  

here can manually give size of string(or number of characters want compare). strlen() may not work length of string because strings not terminated nul character.

update:

see code comparison of unsigned char array

#include<stdio.h> #include<string.h> main() {  unsigned char str[]="gangadhar", str1[]="ganga"; if(!strncmp(str,str1,5)) printf("first 5 charcters of str , str1 same\n"); else printf("not same\n");  } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -