简体   繁体   English

当您在函数中将文字字符串作为参数传递时,C 是否添加了 '\\0'?

[英]Does C add '\0' when you pass a literal string as an argument in a function?

For instance, if I write:例如,如果我写:

void function(char *k){ printf("%s",k);}

and call it like this:并这样称呼它:

function("hello");

does the code translate that string to: "hello\\0" ?代码是否将该字符串转换为: "hello\\0" ? Or I'm the one who has to add it?或者我是必须添加它的人?

In C (And C++), when you do const char* mystr = "Hello";在 C(和 C++)中,当你做const char* mystr = "Hello"; , the compiler will generate the following in (read-only) RAM: ,编译器将在(只读)RAM 中生成以下内容:

0x7fff2fe0: 'H'
0x7fff2fe1: 'e'
0x7fff2fe2: 'l'
0x7fff2fe3: 'l'
0x7fff2fe4: 'o'
0x7fff2fe5: '\0'

Then, the compiler will replace然后,编译器将替换

const char* mystr = "Hello";

with

const char* mystr = 0x7fff2fe0;

For your usage, your code will turn into对于您的使用,您的代码将变成

function(0x7fff2fe0)

Simple as that.就那么简单。

On a compiler level, all string literals have type const char[N] , where the char array is an array that contains all of the written characters, followed by a \\0 .在编译器级别,所有字符串文字都具有const char[N]类型,其中 char 数组是一个包含所有写入字符的数组,后跟一个\\0 The char[N] has a length N that is 1 + the length of the string you write ( char[6] for "Hello" ). char[N]的长度N是 1 + 您编写的字符串的长度( char[6]表示"Hello" )。 More information can be found in the here , where they also use the string "Hello" as an example.可以在此处找到更多信息,他们还以字符串“Hello”为例。 Thus, sizeof("Hello") == 6 , and "Hello"[5] == '\\0' (Yes, "Hello"[5] is legal, remember, "Hello" has type const char[6] ).因此, sizeof("Hello") == 6"Hello"[5] == '\\0' (是的, "Hello"[5]是合法的,记住, "Hello"类型是const char[6] ) . We see this information exemplified in the following:我们在以下示例中看到此信息:

printf("%d\n", sizeof("Hello")); // 6
const char[] str = "Hello"; // Casts from const char[6] to const char[6]
                            // Resulting in a copy of all 6 bytes
printf("%d\n", sizeof(str)); // 6
const char* str2 = "Hello"; // Casts from const char[6] to const char*
printf("%d\n", sizeof(str2)); // 4 on a 32bit system, 8 on a 64bit system

Do note, when casting to a pointer, that you get some pointer eg 0x7fff2fe0 to an array of characters that is not modifiable - attempting to modify the data pointed at 0x7fff2fe0 or 0x7fff2fe5 is explicitly undefined behavior.请注意,在转换为指针时,您会得到一些指针,例如0x7fff2fe0指向不可修改的字符数组 - 尝试修改指向0x7fff2fe00x7fff2fe5的数据是明确未定义的行为。 This status is commonly represented with const ;这种状态通常用const表示; by writing const , the compiler will correctly complain if you try to edit it.通过编写const ,如果您尝试编辑它,编译器将正确地抱怨。

As an additional note, by writing作为附加说明,通过写

char[] myarr = "Hello";

You will create a duplicate stack-allocated character array named myarr , and that array may be modified.您将创建一个名为myarr的重复堆栈分配字符数组,并且可以修改数组。 myarr will indeed still contain \\0 and have a size of 6 char s, in particular, myarr will have type char[6] , with sizeof(myarr) == 6 . myarr确实仍将包含\\0并且大小为 6 char s,特别是myarr将具有类型char[6]sizeof(myarr) == 6

From the C11 Standard Section 6.4.5 String Literals , Paragraph 6 (p. 71):来自C11 标准第 6.4.5 节字符串文字,第 6 段(第 71 页):

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.在转换阶段 7 中,将一个字节或值为零的代码附加到由一个或多个字符串文字产生的每个多字节字符序列。 78) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence 78) 然后使用多字节字符序列初始化一个静态存储持续时间和长度刚好足以包含该序列的数组

A string literal already includes a terminating \\0 by itself, regardless of what you do with that literal.字符串文字本身已经包含一个终止\\0 ,无论您如何处理该文字。 "hello" is always a char [6] array of h , e , l , l , o and \\0 , by definition. "hello"始终是hello\\0char [6]数组。 So, the fact that you "pass it to a function" is completely inconsequential here.因此,您“将其传递给函数”这一事实在这里完全无关紧要。

There's no need to add anything.没有必要添加任何东西。

String literals are not passed to the functions only the pointer to the first character.字符串文字不会传递给函数,只有指向第一个字符的指针。 The referenced object will have all the chars + terminating zero.引用的对象将具有所有字符 + 终止零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串文字作为参数传递给打印 function 而不使用任何格式说明符? - Pass string literal as the argument to the print function with out any format specifier? 如何通过c中的字符串连接创建一个字符串文字作为函数参数 - how to create a string literal as a function argument via string concatenation in c 当您将字符串传递给 C 中的函数时,幕后会发生什么 - What happens under the hood when you pass a string to a function in C 在函数中作为参数传递时,字符串文字参数如何解释? - How is the string literal argument interpreted when passed as parameter in function? C:将字符串数组作为函数参数传递 - C : Pass Array of string as function argument 如何传递文字数组作为函数的输入参数? - How to pass literal array as input argument of the function? 做 ';' 以某种方式终止您作为命令行参数传递的字符串? - Does ';' somehow terminates a string that you pass as a command line argument? 传递给函数调用的字符串文字在哪里存储在c中 - where does string literal passed to function call gets stored in c 在C中将字符串文字作为参数传递时的分段错误 - Segmentation Fault in Passing String Literal as Argument in C 我可以使用带有ac-字符串变量而不是字符串文字作为参数的asm函数吗? - Can I use asm function with a c - string variable instead of a string literal as an argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM