字符串文字汇编 [英] Compilation of string literals
问题描述
为什么要用空格,制表符或 \n分隔的两个字符串文字正确无误地编译?
Why can two string literals separated by a space, tab or "\n" be compiled without an error?
int main()
{
char * a = "aaaa" "bbbb";
}
aaaa是一个字符*
bbbb是一个char *
"aaaa" is a char* "bbbb" is a char*
没有特定的串联规则来处理两个字符串文字。很显然,以下代码在编译期间会出现错误:
There is no specific concatenation rule to process two string literals. And obviously the following code gives an error during compilation:
#include <iostream>
int main()
{
char * a = "aaaa";
char * b = "bbbb";
std::cout << a b;
}
此级联对所有编译器通用吗? aaaa的零终止在哪里?
Is this concatenation common to all compilers? Where is the null termination of "aaaa"? Is "aaaabbbb" a continuous block of RAM?
推荐答案
如果看到例如此翻译阶段参考在第6阶段中确实做到了:
If you see e.g. this translation phase reference in phase 6 it does:
将相邻的字符串文字串联在一起。
Adjacent string literals are concatenated.
这就是这里发生的情况。您有两个相邻的字符串文字,它们被串联为一个字符串文字。
And that's exactly what happens here. You have two adjacent string literals, and they are concatenated into a single string literal.
这是标准行为。
它仅适用于字符串文字,不适用于两个指针变量。
It only works for string literals, not two pointer variables, as you noticed.
这篇关于字符串文字汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!