0

As you guys see I had assigned both of these arrays with the same character in the same index.

#include <iostream>

using namespace std;\

main(){

char a[10] = {0};
char b[10] = {0};
int x;

for(x=0; x<10 ;x++){

    cout << a[x] << " ";

}

for(x=0; x<10 ;x++){

    cout << b[x] << " ";

}


for(x=0; x<4 ;x++){

    a[x]='A';

}

for(x=0; x<4 ;x++){

     b[x]='A';

}


if(a == b)
    cout << "aaaa";

return 0;
}

But both of these arrays are not equal to each other. What did I miss or misunderstood?

  • Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – SuperStormer Jan 09 '21 at 00:55
  • 1
    They are not equal because their addresses are not the same. You are comparing the addresses not the contents. If you use std::string or std::array the comparison would work as you expected. – drescherjm Jan 09 '21 at 00:55
  • 1
    `a == b` does not work for plain C style arrays. The arrays *decay* into pointers to their 1st elements. So you are comparing memory addresses only. And of course the two arrays are located at different addresses. `a == b` does work for `std::string`, though, eg: `#include std::string a = "AAAA", b = "AAAA"; if (a == b) ...` – Remy Lebeau Jan 09 '21 at 00:57
  • 2
    In general arrays are really, really dumb. They are an elegant solution to the problems of the 1970s, but they haven't aged well. – user4581301 Jan 09 '21 at 01:10

0 Answers0