+ 2

C++ - Why are strings equal, but char arrays not?

Given this code: char a[] = "abcde"; char b[] = "abcde"; string c = "abcde"; string d = "abcde"; cout << (c == d); cout << (a == b); It outputs that strings are equal, but char arrays aren't. Why?

10th Sep 2019, 7:57 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
1 Odpowiedź
+ 4
Because std::string is a class with an overloaded == operator that compares character by character. char[] is basically just a pointer that it compares, not the characters.
10th Sep 2019, 8:34 AM
Dennis
Dennis - avatar