kno*_*dge 2 c arrays comparison
我有点困惑.我有以下功能:
int comp(char s1[], char s2[]) {
return s1 == s2;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这只比较了char数组s1和char数组中第一个元素的地址s2.
但奇怪的是,如果我比较(在Visual Studio中)两个相等的char数组
comp("test","test");
Run Code Online (Sandbox Code Playgroud)
我得到1(真)而不是0(假).但是地址不应该不同,因此结果应该始终为0吗?
我想这是使用相同字符串实例进行编译器优化的结果.如果您做了类似这样的事情,您将证明==不起作用,如您所示:
char s1[10];
char s2[10];
strcpy(s1, "test");
strcpy(s2, "test");
printf("%d\n", comp(s1, s2));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |