开发者

null terminator problem while comparing identical char*

i try to compare between 2 char* identical strings,but开发者_开发技巧 one of them contains a null terminator at the end. i've been looking through the internet and understood that it's not recommendable to remove the null terminator char cause it will make the string unstable. what other methods can i use?

the comparing function:

int StringCompare(const char* str1, const char* str2)  
{
    int size1 = strlen(str1), size2 = strlen(str2), min = 0, index =0;  
    bool bigger1 = true;  
    if(size1>size2)  
        min=size2;  
    else  
        min=size1;  
    for(index=0;index<min;index++)  
    {  
        if(str1[index]>str2[index])  
            return 1;  
        if(str1[index]<str2[index])  
            return (-1);  
    }  
    if(size1==size2)  
        return 0;  
    if(min==size1)  
        return (-1);  
    else  
        return 1;  
}    

thanks!


Use std::string, that's what it's for. Failing common sense, use strcmp().


You are using strlen which requires a null terminator at the end of the string. If one of the strings you pass doesn't have a terminator, you are guaranteed to fail.


You're calling strlen on both, so they better both have NULs (not NULL) at the end. Once you get to one of the NUL, you need to stop comparing, because the string is done! That's it! Any subsequent data does not belong to that string.


Please read this posting about C Strings and understand it. ALL c strings requires a nul terminator to signify to the C runtime, that the end of string has been reached. The nul terminator is \0. Notice the distinction between the usage of nul and null, in order to clear any potential confusions - when dealing with strings, its nul, with pointers its NULL!


I would hope that both your char* strings have null terminators at the end! The null terminator is what enables functions like strlen to determine the length, and know when the string ends.


If you know which of the two strings has null terminator, you can call strlen() on it and use that as the length of both strings. But that's a terrible hack - you're not really comparing anything.

Suppose you have two strings:

"abc\0"
"abcdef\0"

Is string 2 null-terminated? Or is "def\0" part of it just random garbage?

The only way to be sure is to null-terminate all strings.


If only one of the strings is null terminated, you should modify your for loop to end when the null is reached for either string, and omit the strlen() call.

for(index=0;;index++)  
{  
    if(!str1[index]) {
        if(!str2[index]) 
            return 0; // strings are equal in length
        return -1; // str1 < str2 since str2 is longer
    }
    if(!str2[index]) 
        return 1;  // str1 > str2 since str1 is longer     

    if(str1[index]>str2[index])  
        return 1;  
    if(str1[index]<str2[index])  
        return (-1);  
}  

Note that this has the same effect as Matteo Italia's answer.


Your code can't possibly work unless both str1 and str2 have null terminators. Otherwise, how do you (or strlen) know how long they are?

If the real question is "How can I work with strings that contain embedded NUL characters?", you should use a vector<char> or similar. You're no longer talking about strings, because by definition strings are NUL-terminated.


If you're trying to compare 2 strings and you cannot be sure they will both be null terminated, you're method signature needs to take the size of the passed strings as arguments, otherwise strlen will tell you how many bytes there are from the char* pointer to the first memory location of 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜