-1

I have written a function to check if two strings are same or not.

int sameString (char string1[], char string2[]) {
    int i = 0;
    while (string1[i] == string2[i]) {
        if (string1[i] == "\0" || string2[i] == "\0") {
            if (string1[i] == "\0" && string2[i] == "\0") {
                return TRUE;
            }
            else {
                return FALSE;
            }
        }
        i++;
    }
}

It is working correctly. But, gcc compiler is giving some warnings which I didn't get.

2.c: In function ‘sameString’:
2.c:10:24: warning: comparison between pointer and integer [enabled by default]
         if (string1[i] == "\0" || string2[i] == "\0") {
                        ^
2.c:10:46: warning: comparison between pointer and integer [enabled by default]
         if (string1[i] == "\0" || string2[i] == "\0") {
                                              ^
2.c:11:28: warning: comparison between pointer and integer [enabled by default]
             if (string1[i] == "\0" && string2[i] == "\0") {
                            ^
2.c:11:50: warning: comparison between pointer and integer [enabled by default]
             if (string1[i] == "\0" && string2[i] == "\0") {
                                                  ^

Also while scanning and saving a string,

char operation[8];
scanf ("%s", &operation);

I am getting one more error, which I didn't get.

2.c: In function ‘main’:
2.c:65:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[8]’ [-Wformat=]
         scanf ("%s", &operation);
         ^

Can someone explain what are these errors?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
rohit15079
  • 213
  • 1
  • 3
  • 14

3 Answers3

1

First, string1[i] is of type char and you're comparing that with a string, "\0". What you need is to

 string1[i] == '\0'

and likewise.

Secondly, it's most likely that operation is an array. You can pass the array name as the argument of %s with scanf(), it decays to the pointer to the first element of the array, automatically.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

You must use character literals instead of string literals

  if (string1[i] == '\0' || string2[i] == '\0') {
                    ^^^^                  ^^^^
        if (string1[i] == '\0' && string2[i] == '\0') {
                          ^^^^                  ^^^^
            return TRUE;
        }

String literals as for example "\0" have types of character arrays. String literal "\0" has type char[2] and consists of two characters { '\0', '\0' }. In expressions with rare exceptions they are converted to pointers to their first characters and have type char *.

Take into account that in general case the function parameters should have the qualifier const.

Also there are redundant conditions inside the function.

The function can be written simpler. For example

int sameString ( const char string1[], const char string2[] ) 
{
    int i = 0;

    while ( string1[i] == string2[i] && string1[i] != '\0' ) i++;

    return string1[i] == string2[i];
}

Instead of the type int for the index it is better to use type size_t or ptrdiff_t.

For example

size_t i = 0;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

In C, a double quoted string is a string (an array of char), while a single quoted string is a single character (char):

#include "stdio.h"

int main (int argc, char **argv ) {
  char* s= "foo";

  // this is comparing the character at s[0] to the location  
  // (pointer) of where the string "f" is stored.
  if (s[0] == "f") {
    printf("s[0] is the same as the location of the string f\n");
  }

  // This is comparing the character at s[0] to the character 'f'
  // which is what you're asking to do.
  if (s[0] == 'f') {
    printf("s[0] is the same as the character f\n");
  }
}

gcc is warning you because although you can do these comparisons (between different types) if you really want to, it is almost certainly not what you meant to do.

Kyle Burton
  • 26,788
  • 9
  • 50
  • 60