gpt4 book ai didi

C 常量字符串相等

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:43 24 4
gpt4 key购买 nike

下面的比较是否保证正确

"hello world"=="hello world";

此外,以下是否总是保证为

char a[] = "hello world";
a == "hello world";

最佳答案

要清楚 - 在这两种情况下,您都在比较指针,而不是实际的字符串内容。

对于

"hello world"=="hello world";

允许比较为truefalse。 C 标准在 6.4.5“String literals”中说:

It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

因此标准允许文字的存储相同或不同。

为了

char a[] = "hello world";
a == "hello world";

比较将始终为false,因为数组a 的地址必须不同于字符串文字的地址。

关于C 常量字符串相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com