0
char a[1][2];
char b[1][2];

a[0][0] = 'a';
a[0][1] = 'b';
b[0][0] = 'a';
b[0][1] = 'b';

if(a[0] == b[0]){
   cout << "worked\n";
}

So as far as I can tell, this comparison between arrays of characters doesn't work the way you would expect it to. The if statement does not execute because the condition a == b returns false. Why is this?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

5

In this statement

if(a == b){
   cout << "worked\n";
}

a and b are implicitly converted to pointers to first elements of the corresponding arrays. So there is a comparison of two pointers. As the arrays occupy different areas in memory then the condition will be alwasy equal to false.

There is no such an operation as the comparison for arrays. To compare two arrays you have toc compare all elements of the arrays with each other. For example you could use standard algorithm std::equal. For example

if( std::equal( std::begin( a ), std::end( a ), std::begin( b ) ) ){
   cout << "worked\n";
}

Another approach is to use standard container std::array that has the comparison operator. For example

std::array<char, 2> a = { 'a', 'b' };
std::array<char, 2> b = { 'a', 'b' };

    if(a == b){
       cout << "worked\n";
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I considered a and b as one-dimensional arrays in the expression in the statement with std::equal. Otherwise the code would be more compound. – Vlad from Moscow Jan 03 '14 at 11:25
4

You can not compare arrays like that. You need to iterate over the arrays and compare each pair of elements in turn. Alternatively(and preferably) replace the static array with std::vector. The code you show compares the pointers a and b which of course are not equal.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • So would the comparison if(*a[0] == *b[0]) work as expected? Is there any downside to using this method? – user3150601 Jan 03 '14 at 11:15
  • @user3150601 no it will not work and this is the main downside. – Ivaylo Strandjev Jan 03 '14 at 11:15
  • I edited the code and my question, does this refined one work? – user3150601 Jan 03 '14 at 11:16
  • No it does not `a[0]` is still an array. You can not compare arrays like that. However you can use `strcmp` to compare character arrays. – Ivaylo Strandjev Jan 03 '14 at 11:17
  • @user3150601, [Arrays](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) (please read that) decay into a pointer to the first element in a few circumstances. Therefore, you are comparing the first character only with both `*a` and `a[0]`. – chris Jan 03 '14 at 11:18
  • @chris thank you! I'll check it out (and strcmp seems to be what I'm looking for here). – user3150601 Jan 03 '14 at 11:19
  • @chris not the first character but the address of the first character in the cases you show. – Ivaylo Strandjev Jan 03 '14 at 11:19
  • @IvayloStrandjev, Woops, I forgot it was a 2D array. That's right, so the first element is again, an array. The `*a[0]`, posted in the first comment, would then compare the first character only. – chris Jan 03 '14 at 11:21
  • @chris in fact OP edited the content of the comment it used to be `a[0] == b[0]` – Ivaylo Strandjev Jan 03 '14 at 11:22
  • @IvayloStrandjev, Good point, I had a nudge in the back of my mind that it used to be different. – chris Jan 03 '14 at 11:23
  • @IvayloStrandjev `strcmp` won't work in his case; `strcmp` requires `'\0'` terminated char sequences, _not_ just any character array. (In this special case, `memcmp` would work.) – James Kanze Jan 03 '14 at 11:30
  • Well in case there is no terminating `\0` you may either use `strncmp` or `memcmp` as you suggest. Still I think using `string` or `vector` is a way better option – Ivaylo Strandjev Jan 03 '14 at 11:33