0
#include <stdio.h>
#include <stdlib.h>

int main()
{
char array1 [50];
char array2 [50];
printf("enter a string:\n");
fgets(array1, 50, stdin);
printf("enter a string2:\n");
fgets(array2, 50, stdin);
if (array1==array2){
    printf("True");
}

}

If I enter the same string twice this code should print true. However it doesn't. How do I fix this?

Salgar
  • 7,687
  • 1
  • 25
  • 39
user3078379
  • 19
  • 2
  • 6
  • Arrays are pointer. Your `==` compares the address locations and not the char elements – Micka Apr 11 '14 at 18:50
  • 1
    @Micka: No, arrays are not pointers. Read section 6 of the [comp.lang.c FAQ](http://www..c-faq.com). You're correct that `array1==array2` does a pointer comparison, but only because the array expressions are *converted* to pointers. – Keith Thompson Apr 11 '14 at 18:52
  • use the function [strncmp](http://www.cplusplus.com/reference/cstring/strncmp/). This link is for C++, but works the same in C. – eventHandler Apr 11 '14 at 18:53
  • @eventHandler: Why `strncmp` rather than `strcmp`? – Keith Thompson Apr 11 '14 at 18:53
  • yeah that's better. i was comparing some kind of strings yesterday using strncmp. got confused. [strcmp](http://www.cplusplus.com/reference/cstring/strcmp/) suits better for this. – eventHandler Apr 11 '14 at 18:56
  • Shall it be a byte-string, aka no interpretation except for the terminator, or text in some encoding like UTF-8? – Deduplicator Apr 11 '14 at 19:00

6 Answers6

2

To compare strings you can use strcmp it is defined in string.h

array1 and array2 are pointers to the start of the strings. thus this code array1==array2 compares pointers not the strings.

a5hk
  • 7,532
  • 3
  • 26
  • 40
1

Use strcmp like

 if (!strcmp(array1,array2))
    printf("true\n");

See strcmp(3)

Your comparison array1 == array2 is comparing addresses (so does not work).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

You are comparing the two pointers to the memory location of the arrays.

You can use strcmp:

if (strcmp(str1,str2) == 0) {
    printf("they match!");
}

Or in more detail, you need to compare the value of each entry in the arrays:

int i;
int match = 1;
for (i = 0; i < 50; i++) {
    if (array1[i] != array2[i]) {
        match = 0;
        break;
    }
}
if (match == 1) {
    printf("They match");
} else {
    printf("They do not match");
}
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
0

You're comparing two pointers there. You want strncmp(array1, array2, 50) == 0

Salgar
  • 7,687
  • 1
  • 25
  • 39
0

You should use strcmp or strncmp.

For example:

int result = strncmp(string1, string2, compareLimit);, where compareLimit is the number of characters to compare to.

The result will inform you on equality 0 or whether string1 is greater than, or less than string2.

bblincoe
  • 2,393
  • 2
  • 20
  • 34
0

It depends on what you mean by the same. Do you want the exact same byte sequence, or a locale-dependend comparison?

The first one is handled be strcmp(), the second calls for strcoll(). Both are in <string.h>.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118