char *str1 = "hello";
char str2[] = "hello";
if (str1 == "hello") printf("ok1\n"); // true
if (str2 == "hello") printf("ok2\n"); // false
if (strcmp(str1, "hello") == 0) printf("ok3\n"); // true
if (strcmp(str2, "hello") == 0) printf("ok4\n"); // true
char *str1 = "hello":str1 指向**字串常量區**的 "hello",編譯器優化使相同字面值共用同一記憶體位址,故 str1 == "hello" 為真。char str2[] = "hello":在堆疊分配陣列並**複製**字面值內容,str2 指向堆疊位址,與常量區位址不同,故 str2 == "hello" 為假。strcmp 比較**內容**,兩者皆為真。永遠使用 strcmp / std::string::operator== 比較字串內容,切勿使用 == 比較 char* 或陣列名。