C:字符串文字 - 问题

发布于 2024-10-26 09:30:03 字数 513 浏览 8 评论 0原文

仅在 char *ptr = "Hello World" 中是字符串文字还是两者都是 "Hello World" 字符串文字?

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char array[] = { "Hello World" };
    char *ptr = "Hello World";

    printf( "%s\n", array );
    printf( "%s\n", ptr );

    printf( "%c\n", *array );
    printf( "%c\n", *ptr );

    printf( "%c\n", array[1] );
    printf( "%c\n", ptr[1] );

    return EXIT_SUCCESS;
}

# Hello World
# Hello World
# H
# H
# e
# e

Is only in char *ptr = "Hello World" a string literal or are both "Hello World" string literals?

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char array[] = { "Hello World" };
    char *ptr = "Hello World";

    printf( "%s\n", array );
    printf( "%s\n", ptr );

    printf( "%c\n", *array );
    printf( "%c\n", *ptr );

    printf( "%c\n", array[1] );
    printf( "%c\n", ptr[1] );

    return EXIT_SUCCESS;
}

# Hello World
# Hello World
# H
# H
# e
# e

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

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

发布评论

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

评论(7

月寒剑心 2024-11-02 09:30:03

"Hello World" 是一个字符串文字 - 无论是将其分配给 char * 还是将其复制到 char []

char * ptr = "Hello World"; 实际上应该是 const char * ptr = "Hello World"; - 字符串文字可能是不可修改的(在只读内存中),并且使用 < code>const char * 是额外的保护措施反对修改。

char array[] = { "Hello World" }; 是安全的 - 这会将字符串文字从潜在的只读内存复制到局部变量中。

"Hello World" is a string literal - no matter whether you assign it to a char * or copy it to a char []

char * ptr = "Hello World"; should really be const char * ptr = "Hello World"; - String literals may be unmodifiable (in readonly memory), and using a const char * is an extra safeguard against modification.

char array[] = { "Hello World" }; is safe - this copies the string literal from the potential readonly memory into a local variable.

太阳男子 2024-11-02 09:30:03

"Hello world" 始终是一个字符串文字,无论您如何处理它。

char *ptr = "Hello World"; 定义一个指向通常不可修改的空间的指针(因此您应该像 const char * 一样处理它,并且您的编译器可能会如果你不这样做的话,实际上会抱怨)。

char array[] = { "Hello World" }; 在堆栈上创建数组,因此它是可修改的并且大致等于 char array[sizeof("Hello World")]; strcpy(数组, "Hello World");

"Hello world" is always a string literal, no matter what you do with it.

char *ptr = "Hello World"; defines a pointer which points to space which is often not modifiable (so you should handle it like a const char * and your compiler might actually complain if you don't).

char array[] = { "Hello World" }; creates the array on the stack so it's modifiable and roughly equal to char array[sizeof("Hello World")]; strcpy(array, "Hello World");

鹤仙姿 2024-11-02 09:30:03

"Hello World" 是一个字符串文字。

char *ptr = "Hello World"; 的情况下,ptr 指向存在于只读位置的 "Hello World"。任何通过 ptr 修改 it 的尝试都会调用未定义的行为。

而在 char array[] = { "Hello World" }; 中,字符串文字的内容被复制到堆栈上。您可以在不调用 UB 的情况下修改这些内容。

BTW ISO C99(第 6.4.5/6 节)说

如果这些数组的元素具有适当的值,则未指定这些数组是否不同。

这意味着编译器是否将两次(或多次)出现的“Hello World”视为不同的情况是未指定的。

"Hello World" is a string literal.

In case of char *ptr = "Hello World"; ptr is pointing to "Hello World" which is present in read only location. Any attempt to modify the it via ptr would invoke undefined behaviour.

Whereas in char array[] = { "Hello World" }; content of the string literal is copied onto a stack. You are allowed to modify those content without invoking UB.

BTW ISO C99 (Section 6.4.5/6) says

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

That means it is unspecified whether the compiler treats two(or more) occurrences of "Hello World" distinct.

洋洋洒洒 2024-11-02 09:30:03

只有“Hello World”本身是字符串文字。

ptr 是一个指向 char 的指针,使用字符串文字的第一个元素的地址进行初始化。

array 是一个数组,而不是一个文字。

Only the "Hello World" themselves are string literals.

ptr is a pointer to char initialized with the address of the first element of the string literal.

array is an array and not a literal.

美人如玉 2024-11-02 09:30:03

"Hello World" 是一个字符串文字,无论它出现在代码中的哪个位置。 ptrarray 都不是字符串文字。

"Hello World" is a string literal, regardless of where in the code it appears. Neither ptr nor array are string literals.

小情绪 2024-11-02 09:30:03

它被编译为只读部分中的字符串文字。

        .section        .rodata
.LC0:
        .string "Hello World"

两次出现都被组合成一个数组......至少在我的带有 gcc 的机器上是这样。

This is compiled to a string literal in the read only section.

        .section        .rodata
.LC0:
        .string "Hello World"

Both occurences are combined into one array... at least on my machine with gcc.

温柔少女心 2024-11-02 09:30:03

当您声明 char array[] 时,您正在声明一个字符数组(可以读取和写入),并且该数组被初始化为某个字符序列

如果您声明 char * ptr,您正在声明一个指向的指针直接到一些常量文字(不是副本),你可以直接读取它

When you declare char array[] you are declaring an array of chars (which is accessible to be both read and written), and this array is initialized to some sequence of characters

If you declare char * ptr, you are declaring a pointer that points directly to some constant literal(not a copy) and you can just read it

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