C- 比较运算符而不是 strcmp
C- Comparison operator instead of strcmp
如果我在 C 中使用比较运算符而不是 strcmp 来比较字符串,会发生什么情况?它会比较它的 ASCII 值和 return 结果吗?
它将比较两个指针的地址。
所以:
char* a = "hello";
char* b = "test";
char* c = "hello";
char* d = a;
a == d; // true
a == b; // false
a == c; // true or false, depending on the compiler's behavior.
如果编译器决定回收 "hello" 的实际字符串数据,第三个示例将是正确的,但它没有义务这样做。
如果我在 C 中使用比较运算符而不是 strcmp 来比较字符串,会发生什么情况?它会比较它的 ASCII 值和 return 结果吗?
它将比较两个指针的地址。
所以:
char* a = "hello";
char* b = "test";
char* c = "hello";
char* d = a;
a == d; // true
a == b; // false
a == c; // true or false, depending on the compiler's behavior.
如果编译器决定回收 "hello" 的实际字符串数据,第三个示例将是正确的,但它没有义务这样做。