-2

I am trying to compare the string literal values of two char* of different lengths. They don't have null terminators so I can't use strcmp. How should I determine if they are equal? Is there a method that I could use?

Example code:

int main(){
    char* one = "milk";
    char* two = "dalek! Exterminate!";
    char* three = "milk";

    //Compare and check to see if they are equal. one and two would return false but one and three would return true

}
  • 1
    `char* c = "..."` is not well-formed. A string literal has the type `const char*`. What /exactly/ are you trying to do? – user123 Jul 16 '14 at 01:16
  • 1
    The stings in your example **do** have null terminators (they are implicit in string literals). Are you sure in your real code the strings really don't have null terminators? – sth Jul 16 '14 at 01:17
  • @sth when I do strcmp, I get a segmentation fault:11 . I read somewhere that that means that my char* didn't have null terminators – user3689979 Jul 16 '14 at 01:19
  • I think a string literal and can be used in strcmp without problems. please post an example of cause segfault. – BLUEPIXY Jul 16 '14 at 01:24
  • 2
    There can be lots of reasons for a segfault. Determine where exactly it happens (a debugger will tell you) and post the code related to the problematic line of code. – sth Jul 16 '14 at 01:34
  • 1
    Shall that be counted strings? Because then you accepted a bad answer... – Deduplicator Jul 16 '14 at 01:40
  • ... was a bad answer when you accepted. Now it's good. – Deduplicator Jul 16 '14 at 01:58
  • @MohammadAliBaydoun String literals have type `char[]`, and it is well-formed to assign them to a `char *`. See C99 6.4.5#5 – M.M Jul 16 '14 at 05:42

1 Answers1

2

You can use memcmp to compare the strings up to the point where one of them ends.

int strcmpNoTerminator ( const char * str1, const char * str2, size_t str1len, size_t str2len ) {
    // Get the length of the shorter string
    size_t len = str1len < str2len ? str1len : str2len;
    // Compare the strings up until one ends
    int cmp = memcmp(str1, str2, len);
    // If they weren't equal, we've got our result
    // If they are equal and the same length, they matched
    if(cmp != 0 || str1len == str2len) {
        return cmp;
    }
    // If they were equal but one continues on, the shorter string is 
    // lexicographically smaller
    return str1len < str2len ? -1 : 1;
}

Note that this is if your char *s are actually not null terminated. In your example code, one, two, and three are null terminated. I'm assuming that your question itself is correct, and not your example. If the example is correct, then your char *s are null terminated and your problem lies elsewhere, in which case we'd need to see more code to help.

Mike Precup
  • 4,148
  • 21
  • 41
  • And why is this wrong? He said there are no null terminators, `\0`s shouldn't be an issue. Even if there were null terminators, this should be fine. The only time this should be an issue is if there are random `\0`s littered throughout the string. – Mike Precup Jul 16 '14 at 01:49
  • Seemed like a fair assumption, but since the question didn't specify, I'll add in something for it. – Mike Precup Jul 16 '14 at 01:55
  • Sorry for not using a proper character literal in my first comment and so removing all ambiguity. Anyway, change it to `memcmp` and all is well. – Deduplicator Jul 16 '14 at 01:55
  • Maybe helpful in this context: You can initialize `char` _arrays_ without 0-terminator if you give an explicit length, e.g. `char foo[3] = "foo";`. This may be easier to use in combination with `sizeof` because you don't have to subtract 1 to get the actual size (and you save one byte, yeah). – mafso Jul 16 '14 at 02:06
  • Wouldn't it make much more sense to do the length equality check first and avoid the memcmp call altogether when `str1len != str2len`? – Dan Bechard Dec 04 '17 at 06:31
  • @Dan Only if you're just checking for equality, in which case this function could be a clean one-liner. Assuming you want to mimic the interface of `strcmp`, the `memcmp` call is required even if the lengths match. – Mike Precup Feb 09 '18 at 01:53
  • @MikePrecup Ah right, dunno what I was thinking. Thanks for clarifying. – Dan Bechard Feb 09 '18 at 22:16