如果不是简单的字符数组,C 中的字符串是什么?
我是 C 编程语言的新手,我的印象是字符串只是字符数组。但是,当我尝试下面的代码(以及其他一些测试)时:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char apple1[] = { 'a', 'p', 'p', 'l', 'e', '\0' };
char *apple2 = "apple";
char apple3[] = "apple";
printf("%i\n", apple1 == apple2); // 0
printf("%i\n", apple2 == apple3); // 0
printf("%i\n", apple3 == apple1); // 0
printf("%i\n", "apple" == apple1); // 0
printf("%i\n", "apple" == apple2); // 1
printf("%i\n", "apple" == apple3); // 0
printf("%i\n", !strcmp(apple1, apple2)); // 1
for (size_t i = 0; i < strlen(apple) + 1; i++)
{
printf("%i", apple1[i] == apple2[i]);
} // 111111
return 0;
}
我得到了一些意想不到的结果。至少对我来说,这些违反直觉的结果有什么原因吗?非常感谢。
I'm new to the C programming language, and I was under the impression that strings are just arrays of characters. However, when I tried the following code below (among some other tests):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char apple1[] = { 'a', 'p', 'p', 'l', 'e', '\0' };
char *apple2 = "apple";
char apple3[] = "apple";
printf("%i\n", apple1 == apple2); // 0
printf("%i\n", apple2 == apple3); // 0
printf("%i\n", apple3 == apple1); // 0
printf("%i\n", "apple" == apple1); // 0
printf("%i\n", "apple" == apple2); // 1
printf("%i\n", "apple" == apple3); // 0
printf("%i\n", !strcmp(apple1, apple2)); // 1
for (size_t i = 0; i < strlen(apple) + 1; i++)
{
printf("%i", apple1[i] == apple2[i]);
} // 111111
return 0;
}
I got some unexpected results. Is there any reason for these, at least for me, counterintuitive results? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这些声明中,
声明了两个数组 apple1 和 apple3,其中包含字符串“apple”。
在此声明中,
声明了一个指向字符串文字
“apple”
的指针。在 printf 的这些调用中,
比较了占用不同内存范围的不同数组的第一个字符的地址。表达式中使用的数组(极少数例外)将转换为指向其第一个元素的指针。因此,表达式的结果是整数值 0,但此调用除外
,因为在这种情况下,存在指向同一字符串文字(其第一个字符)的比较指针,因为编译器似乎分配了一个字符数组来存储使用的字符串文字“apple”在此调用和此指针声明中,
您可以通过以下方式表示上述声明和 printf 的调用
,但是一般来说,即使您会编写示例
,输出也可以是 0 或 1,具体取决于编译器如何存储相同的值字符串文字:作为不同的字符数组或作为一个字符数组(这取决于编译器选项)。
要比较包含字符串的字符数组,您需要使用标准 C 字符串函数
strcmp
。In these declarations
there are declared two arrays apple1 and apple3 that contain the string
"apple"
.In this declaration
there is declared a pointer to the string literal
"apple"
.In these calls of printf
there are compared addresses of first characters of different arrays that occupy different extents of memory. Arrays used in expressions with rare exceptions are converted to pointers to their first elements. So the result of the expressions is the integer value 0 except this call
because in this case there are compared pointers to the same string literal (its first character) because it seems the compiler allocated one character array to store the string literal "apple" used in this call and in this declaration of a pointer
You can represent the above declaration and the call of printf the following way
However in general even if you will write for example
then the output can be either 0 or 1 depending on how the compiler stores identical string-literals: either as different character arrays or as one character array (it depends on compiler options).
To compare character arrays that contain strings you need to use standard C string function
strcmp
.