如果不是简单的字符数组,C 中的字符串是什么?

发布于 2025-01-14 05:47:29 字数 810 浏览 1 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(1

茶底世界 2025-01-21 05:47:29

在这些声明中,

char apple1[] = { 'a', 'p', 'p', 'l', 'e', '\0' };
char *apple2 = "apple";
char apple3[] = "apple";

声明了两个数组 apple1 和 apple3,其中包含字符串“apple”。

在此声明中,

char *apple2 = "apple";

声明了一个指向字符串文字“apple”的指针。

在 printf 的这些调用中,

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

比较了占用不同内存范围的不同数组的第一个字符的地址。表达式中使用的数组(极少数例外)将转换为指向其第一个元素的指针。因此,表达式的结果是整数值 0,但此调用除外

printf("%i\n", "apple" == apple2); // 1

,因为在这种情况下,存在指向同一字符串文字(其第一个字符)的比较指针,因为编译器似乎分配了一个字符数组来存储使用的字符串文字“apple”在此调用和此指针声明中,

char *apple2 = "apple";

您可以通过以下方式表示上述声明和 printf 的调用

char *apple2 = &"apple"[0];
//...
printf("%i\n", &"apple"[0] == apple2); // 1

,但是一般来说,即使您会编写示例

printf("%i\n", "apple" == "apple");

,输出也可以是 0 或 1,具体取决于编译器如何存储相同的值字符串文字:作为不同的字符数组或作为一个字符数组(这取决于编译器选项)。

要比较包含字符串的字符数组,您需要使用标准 C 字符串函数 strcmp

In these declarations

char apple1[] = { 'a', 'p', 'p', 'l', 'e', '\0' };
char *apple2 = "apple";
char apple3[] = "apple";

there are declared two arrays apple1 and apple3 that contain the string "apple".

In this declaration

char *apple2 = "apple";

there is declared a pointer to the string literal "apple".

In these calls of printf

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

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

printf("%i\n", "apple" == apple2); // 1

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

char *apple2 = "apple";

You can represent the above declaration and the call of printf the following way

char *apple2 = &"apple"[0];
//...
printf("%i\n", &"apple"[0] == apple2); // 1

However in general even if you will write for example

printf("%i\n", "apple" == "apple");

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.

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