C:字符串文字 - 问题
仅在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
"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 achar *
or copy it to achar []
char * ptr = "Hello World";
should really beconst char * ptr = "Hello World";
- String literals may be unmodifiable (in readonly memory), and using aconst 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."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 aconst 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 tochar array[sizeof("Hello World")]; strcpy(array, "Hello World");
"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 viaptr
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
That means it is unspecified whether the compiler treats two(or more) occurrences of
"Hello World"
distinct.只有“Hello World”本身是字符串文字。
ptr
是一个指向char
的指针,使用字符串文字的第一个元素的地址进行初始化。array
是一个数组,而不是一个文字。Only the
"Hello World"
themselves are string literals.ptr
is a pointer tochar
initialized with the address of the first element of the string literal.array
is an array and not a literal."Hello World"
是一个字符串文字,无论它出现在代码中的哪个位置。ptr
和array
都不是字符串文字。"Hello World"
is a string literal, regardless of where in the code it appears. Neitherptr
norarray
are string literals.它被编译为只读部分中的字符串文字。
两次出现都被组合成一个数组......至少在我的带有 gcc 的机器上是这样。
This is compiled to a string literal in the read only section.
Both occurences are combined into one array... at least on my machine with gcc.
当您声明 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