从字符串文字初始化char数组时会发生什么? [英] What happens when a char array gets initialized from a string literal?
问题描述
据我了解,以下代码的工作方式如下:
As I understand it, the following code works like so:
char* cptr = "Hello World";
"Hello World"位于程序内存的 .rodata
部分中.字符串文字"Hello World"
返回指向字符串基地址或所谓数组"中第一个元素的地址的指针,因为字符在内存中顺序排列这将是"H".这是我的小图,当我看到存储在内存中的字符串文字时:
"Hello World" lives in the .rodata
section of the program's memory. The string literal "Hello World"
returns a pointer to the base address of the string, or the address of the first element in the so-called "array", since the chars are laid out sequentially in memory it would be the 'H'. This is my little diagram as I visualize the string literal getting stored in the memory:
0x4 : 'H'
0x5 : 'e'
0x6 : 'l'
0x6 : 'l'
0x7 : 'o'
0x8 : ' '
0x9 : 'W'
0xa : 'o'
0xb : 'r'
0xc : 'l'
0xd : 'd'
0xe : '\0'
因此上面的声明变为:
char* cptr = 0x4;
现在cptr指向字符串文字.我只是在补地址.
Now cptr points to the string literal. I'm just making up the addresses.
0xa1 : 0x4
现在这段代码如何工作?
Now how does this code work?
char cString[] = "Hello World";
我假设像以前的情况一样,"
I am assuming that as in the previous situation "Hello World"
also degrades to the address of 'H' and 0x4.
char cString[] = 0x4;
当我将 =
与char数组的初始化一起使用时,我正在阅读它作为重载的赋值运算符.据我了解,仅在初始化C字符串时,它将从给定基地址开始的逐字符复制到C字符串中,直到最后复制的字符达到"\ 0"为止.它还为所有字符分配足够的内存.因为重载运算符实际上只是函数,所以我认为它的内部实现类似于 strcpy()
.
I am reading the =
as an overloaded assignment operator when it used with initialization of a char array. As I understand, at initialization of C-string only, it copies char-by-char starting at the given base address into the C-string until it hits a '\0' as the last char copied. It also allocates enough memory for all the chars. Because overloaded operators are really just functions, I assume that it's internal implementation is similar to strcpy()
.
我希望有一位经验更丰富的C程序员来确认我对该代码如何工作的假设.这是我将字符串文字中的字符复制到C字符串后的可视化效果:
I would like one of the more experienced C programmers to confirm my assumptions of how this code works. This is my visualization of the C-string after the chars from the string literal get copied into it:
0xb4 : 'H'
0xb5 : 'e'
0xb6 : 'l'
0xb6 : 'l'
0xb7 : 'o'
0xb8 : ' '
0xb9 : 'W'
0xba : 'o'
0xbb : 'r'
0xbc : 'l'
0xbd : 'd'
0xbe : '\0'
再一次,地址是任意的,关键是堆栈中的C字符串与内存中 .rodata
部分中的字符串文字不同.
Once again, the addresses are arbitrary, the point is that the C-string in the stack is distinct from the string literal in the .rodata
section in memory.
我要做什么?我试图使用一个char指针来临时保存字符串文字的基地址,并使用相同的char指针(字符串文字的基地址)来初始化C字符串.
What am I trying to do? I am trying to use a char pointer to temporarily hold the base address of the string literal, and use that same char pointer (base address of string literal) to initialize the C-string.
char* cptr = "Hello World";
char cString[] = cptr;
我假定"Hello World"
的计算结果为其基地址 0x4
.因此,这段代码应如下所示:
I assume that "Hello World"
evaluates to its base address, 0x4
. So this code ought to look like this:
char* cptr = 0x4;
char cString[] = 0x4;
我认为它应该与 char cString [] ="Hello World";
相同,因为"Hello World"的计算结果为其基地址,这就是存储在char指针中的内容!
I assume that it should be no different from char cString[] = "Hello World";
since "Hello World" evaluates to its base address, and that is what is stored in the char pointer!
但是,gcc给我一个错误:
However, gcc gives me an error:
error: invalid initializer
char cString[] = cptr;
^
- 为什么不能使用char指针作为临时占位符来存储字符串文字的基址?
- 此代码如何工作?我的假设正确吗?
- 在代码中使用字符串文字是否会将基址返回到字符存储在内存中的数组"?
推荐答案
您对内存布局的理解或多或少是正确的.但是您遇到的问题是C语言中的初始化语义之一.
Your understanding of memory layout is more or less correct. But the problem you are having is one of initialization semantics in C.
此处声明中的 =
符号不是赋值运算符.而是,语法为要实例化的变量指定了初始化程序.通常, T x = y;
与 T x不同;x = y;
.
The =
symbol in a declaration here is NOT the assignment operator. Instead, it is syntax that specifies the initializer for a variable being instantiated. In the general case, T x = y;
is not the same as T x; x = y;
.
有一个语言规则,可以从字符串文字中初始化字符数组.(在这种情况下,字符串文字不被评估为它的基地址").没有语言规则,可以从指向要复制到数组中的元素的指针初始化数组.
There is a language rule that a character array can be initialized from a string literal. (The string literal is not "evaluated to its base address" in this context). There is not a language rule that an array can be initialized from a pointer to the elements intended to be copied into the array.
为什么这样的规则?历史原因".
Why are the rules like this? "Historical reasons".
这篇关于从字符串文字初始化char数组时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!