shiran bar
shiran bar

Reputation: 29

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!

Upvotes: 0

Views: 769

Answers (9)

levis501
levis501

Reputation: 4207

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.

Upvotes: 3

user3458
user3458

Reputation:

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.

Upvotes: 3

Paul Whitehurst
Paul Whitehurst

Reputation: 579

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.

Upvotes: 2

Puppy
Puppy

Reputation: 146998

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

Upvotes: 5

RichieHindle
RichieHindle

Reputation: 281795

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.

Upvotes: 2

t0mm13b
t0mm13b

Reputation: 34592

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!

Upvotes: 4

Matt K
Matt K

Reputation: 13872

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.

Upvotes: 4

Anthony Williams
Anthony Williams

Reputation: 68681

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.

Upvotes: 3

Mark Ransom
Mark Ransom

Reputation: 308500

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.

Upvotes: 4

Related Questions