-3

Here is an example:

int main()
{
    char string1[8];
    char string2[7];
    strcpy(string1, "Heloooo");
    strcpy(string2, "Helloo");
    printf("%d", strcmp(string1[2], string2[5]));
    return(0);
}

Won't return anything, even though it should return >0, 0, or <0. If I remove the indexes though like:

    printf("%d", strcmp(string1, string2));

It'll work fine. Can someone tell me what I'm doing wrong here?

LtLame
  • 25
  • 1
  • 5
  • `string1[2]` returns a single character. `strcmp` does not take characters as arguments. – Siguza Dec 09 '17 at 23:32
  • 1
    `string1[2]` and `string2[5]` are `char`s, `strcmp` compares strings. – tkausl Dec 09 '17 at 23:32
  • Or use `strcmp(string1 + 2, string2 + 5);` What is your intent though? – DeiDei Dec 09 '17 at 23:32
  • 1
    As mentioned the [`strcmp`](http://en.cppreference.com/w/c/string/byte/strcmp) function expects to pointers to `char` as arguments. The type of e.g. `string1[2]` is of type `char`, not the expected `char *`. Any good compiler should tell you this (always read your error ***and warning*** messages). I suggest you [get a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and read more. – Some programmer dude Dec 09 '17 at 23:34
  • If you *really* want to compare single characters, you *can* do `string1[2] == string2[5]`. – Bo Persson Dec 09 '17 at 23:37
  • If your compiler didn't complain about the abuse of `strcmp()`, you are misusing your compiler. Either turn on the warnings, or get a better compiler. You (like everyone else, me included — me especially) need all the help you can get from the compiler. Don't scorn its advice (and any warning it generates is salient advice); it knows a lot more about C than you do. – Jonathan Leffler Dec 10 '17 at 00:09

3 Answers3

1

strcmp takes a pair of character pointers, while string1[x] is not a pointer, but a character:

printf("%d", strcmp(&string1[2], &string2[5]));

or

printf("%d", strcmp(string1+2, string2+5));

Note that although string1 and string2 are arrays, C compiler converts them to character pointers without additional operators.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The function strcmp expects pointers as the arguments. See the prototype: int strcmp(const char * s1, const char * s2);

string1[2] and string2[5] are just characters 'l' and 'o'.

printf("%d", strcmp(string1[2], string2[5])); // will not compile 

printf("%d", strcmp(&string1[2], &string2[5])); // will return -3 with my compiler GCC 5.3.0

&string1[2] is a pointer to string1[2] character

&string2[5] is a pointer to string2[5] character

Note:

The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

However, the exact return value depends on the implementation.

For this implementation:

int strcmp_fast (const char * s1, const char * s2)
{
    for(; *s1 == *s2; ++s1, ++s2)
        if(*s1 == 0)
            return 0;
    return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}

the:

printf("%d", strcmp_fast(&string1[2], &string2[5])); 

printed value is -3.

sg7
  • 6,108
  • 2
  • 32
  • 40
0

str[ndx] evaluates to a char but the function expects a char *,[] produces an l-value so you can take its address &str[ndx] and get what you are after.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23