0

I wrote one simple function that should return wheather the two strings supplied by the user are identical. Namely, it should return 1 if they are and 0 if they aren't.

Here is a simple runnable program with that function, please check it and point out what is wrong.

#include <stdio.h>

int compare_strings(char string0[], char string1[])
{
        int success = 0;

        if (sizeof(string0)/sizeof(char) == sizeof(string1)/sizeof(char))
        {   
                success = 1;
                for (int i = 0;i < sizeof(string0)/sizeof(char);i++)
                {   
                        if (string0[i] != string1[i])
                        {   
                                success = 0;
                                break;
                        }   
                }   
        }   

        return success;
}

void main(int argc, char **argv)
{
        if (argc >= 3)
        {   
                int yes_or_no = compare_strings(argv[1], argv[2]);
                if (yes_or_no == 0)
                {   
                        printf("FALSE\n");
                }   
                else
                {   
                        printf("TRUE\n");
                }   
        }   
        else
        {   
                printf("I need at least two parameters!\n");
        }   
}
Hanlon
  • 330
  • 1
  • 11
  • sizeof does measure string length. – FBergo May 05 '18 at 19:56
  • Number of chars in string or length in memory? – Hanlon May 05 '18 at 19:58
  • You should have noted during compilation that you get something like `warning: sizeof on array function parameter will return size of 'char *' instead of 'char []' [-Wsizeof-array-argument]`, which means that you end up only checking the first `sizeof(char *)` characters of each string rather than what you expect. Use `strlen()` instead of `sizeof() / sizeof(char)` – Patrick Roberts May 05 '18 at 19:58
  • 1
    Something to read: https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Deduplicator May 05 '18 at 20:05

1 Answers1

3

This only determines the size of a pointer to char, which is what an array of char decays to as a function parameter.

sizeof(string0)

In order to do the string length comparison (assuming of course 0-terminated sequences of char) you need to use

strlen(string0)

Then this only compares the pointers to different strings, i.e. address where they are stored, not their content. For two different arrays this is guaranteed to be different, i.e. the expression with != will always be true.

string0[i] != string1[i]

In order to compare two 0-terminated char sequences (as the argv are guaranteed to be), use

0==strcmp(string0, string1)
Yunnosch
  • 21,438
  • 7
  • 35
  • 44
  • For `argv[x]`, strings will always be null-terminated, so you can just use `strcmp()` without the max length parameter. – Patrick Roberts May 05 '18 at 20:03
  • Hmpf. Decay and interpreting function parameters are related but not identical. – Deduplicator May 05 '18 at 20:04
  • @Deduplicator Feel free to elaborate. I will pick it up and improve my answer. Or add an answer with those details, I will be happy to upvote something which teaches me something. – Yunnosch May 05 '18 at 20:05