将字符数组与 C 中的 == 运算符进行比较

发布于 2024-08-13 07:41:10 字数 629 浏览 4 评论 0原文

我知道在 C 中比较“字符串”的正确方法是使用 strcmp,但现在我尝试将一些字符数组与 == 运算符进行比较,并得到一些奇怪的结果结果。

看一下下面的代码:

int main()
{
    char *s1 = "Andreas";
    char *s2 = "Andreas";

    char s3[] = "Andreas";
    char s4[] = "Andreas";

    char *s5 = "Hello";

    printf("%d\n", s1 == s2); //1
    printf("%d\n", s3 == s4); //0
    printf("%d\n", s1 == s5); //0
}

第一个 printf 正确打印 1,这表明它们不相等。但是有人可以向我解释为什么在比较字符数组时 == 返回 0 吗?

有人可以向我解释为什么第一个 < code>printf 返回 1 (即它们相等),而字符数组返回 0

I know that the correct way to compare "strings" in C is by using strcmp, but now I tried comparing some character arrays with the == operator, and got some strange results.

Take a look at the following code:

int main()
{
    char *s1 = "Andreas";
    char *s2 = "Andreas";

    char s3[] = "Andreas";
    char s4[] = "Andreas";

    char *s5 = "Hello";

    printf("%d\n", s1 == s2); //1
    printf("%d\n", s3 == s4); //0
    printf("%d\n", s1 == s5); //0
}

The first printf correctly prints a 1, which signals that they are not equal. But can someone explain to me why, when comparing the character arrays, the == is returning a 0 ?

Can someone please explain to me why the first printf is returning a 1 (ie, they are equal) and the character arrays are returning a 0 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅紫色的梦幻 2024-08-20 07:41:10

== 是比较内存地址。
您的编译器可能使 s1 和 s2 指向相同的静态数据以节省空间。

IE。前两行代码中的“Andreas”存储在您的可执行数据中。 C 标准表示这些字符串是常量,因此优化了这两个指针以指向同一存储空间。

char[] 行通过将数据复制到变量中来创建变量,因此在执行期间存储在堆栈上的不同地址。

The == is comparing the memory address.
It's likely that your compiler is making s1 and s2 point to the same static data to save space.

ie. The "Andreas" in the first two lines of code is stored in your executable data. The C standard says these strings are constant and so has optomized the two pointers to point to the same storage.

The char[] lines create a variable by copying the data into the variable and so are stored at different address on the stack during execution.

终陌 2024-08-20 07:41:10

呃...当 == 打印 1 时,意味着它们相等。它与 strcmp 不同,strcmp 返回字符串的相对顺序。

Uh... when == prints a 1, it means they are equal. It's different from strcmp, which returns the relative order of the strings.

始于初秋 2024-08-20 07:41:10

您正在比较地址而不是字符串。前两个是常量,只会创建一次。

int main()
{
    char *s1 = "Andreas";
    char *s2 = "Andreas";

    char s3[] = "Andreas";
    char s4[] = "Andreas";

    char *s5 = "Hello";

    printf("%d\n", s1 == s2); //1
    printf("%p == %p\n", s1, s2);
    printf("%d\n", s3 == s4); //0
    printf("%p != %p\n", s3, s4);
    printf("%d\n", s1 == s5); //0
    printf("%p != %p\n", s1, s5);
}

在我的计算机上输出,但你明白了:

1
0x1fd8 == 0x1fd8
0
0xbffff8fc != 0xbffff8f4
0
0x1fd8 != 0x1fe0

You are comparing addresses and not the strings. The first two are constant and will only be created once.

int main()
{
    char *s1 = "Andreas";
    char *s2 = "Andreas";

    char s3[] = "Andreas";
    char s4[] = "Andreas";

    char *s5 = "Hello";

    printf("%d\n", s1 == s2); //1
    printf("%p == %p\n", s1, s2);
    printf("%d\n", s3 == s4); //0
    printf("%p != %p\n", s3, s4);
    printf("%d\n", s1 == s5); //0
    printf("%p != %p\n", s1, s5);
}

Output on my computer, but you get the idea:

1
0x1fd8 == 0x1fd8
0
0xbffff8fc != 0xbffff8f4
0
0x1fd8 != 0x1fe0
烟花易冷人易散 2024-08-20 07:41:10

等一下...1 表示 true,0 表示 false。所以你的解释部分是倒退的。至于为什么前两个字符串看起来相等:编译器只构建了一次常量字符串 (s1/2)。

Wait a sec... 1 means true, 0 means false. So your explanation is partially backwards. As for why the first two strings seem to be equal: The compiler built that constant string (s1/2) just once.

转瞬即逝 2024-08-20 07:41:10

s1 == s2 表示“(char*) == (char*)”或者地址相同。

对于 s3 == s4 也是如此。这就是“数组衰减为指针”在起作用。

而你的比较结果的含义是错误的:

0 == 0; /* true; 1 */
42 == 0; /* false; 0 */
"foo" == "bar"; /* false (the addresses are different); 0 */

s1 == s2 means "(char*) == (char*)" or that the addresses are the same.

Same thing for s3 == s4. That's the "arrays decay into pointers" at work.

And you have the meaning of the result of the comparison wrong:

0 == 0; /* true; 1 */
42 == 0; /* false; 0 */
"foo" == "bar"; /* false (the addresses are different); 0 */
顾铮苏瑾 2024-08-20 07:41:10

从 s1 到 s5 的所有值本身都不是 char,它们是char 的指针。因此,您要比较的是每个字符串的内存地址,而不是字符串本身。

如果您这样显示地址,您可以看到比较运算符实际上在做什么:

#include <stdio.h>

int main() {
  char *s1 = "Andreas";
  char *s2 = "Andreas";

  char s3[] = "Andreas";
  char s4[] = "Andreas";

  char *s5 = "Hello";

  printf("%p\n", s1); // 0x80484d0
  printf("%p\n", s2); // 0x80484d0
  printf("%p\n", s3); // 0xbfef9280
  printf("%p\n", s4); // 0xbfef9278
  printf("%p\n", s5); // 0x80484d8
}

字符串在内存中的确切分配位置是特定于实现的。在这种情况下, s1 和 s2 指向相同的静态内存块,但我不希望这种行为是可移植的。

All the values from s1 through s5 aren't char themselves, they're pointers to char. So what you're comparing is the memory addresses of each string, rather than the strings themselves.

If you display the addresses thus, you can see what the comparison operators are actually working on:

#include <stdio.h>

int main() {
  char *s1 = "Andreas";
  char *s2 = "Andreas";

  char s3[] = "Andreas";
  char s4[] = "Andreas";

  char *s5 = "Hello";

  printf("%p\n", s1); // 0x80484d0
  printf("%p\n", s2); // 0x80484d0
  printf("%p\n", s3); // 0xbfef9280
  printf("%p\n", s4); // 0xbfef9278
  printf("%p\n", s5); // 0x80484d8
}

Exactly where the strings are allocated in memory is implementation specific. In this case, the s1 and s2 are pointing to the same static memory block, but I wouldn't expect that behaviour to be portable.

娇女薄笑 2024-08-20 07:41:10

您无法比较字符串,但可以比较指针。

You can't compare strings, but you can compare pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文