简体   繁体   English

在函数中作为参数传递时,字符串文字参数如何解释?

[英]How is the string literal argument interpreted when passed as parameter in function?

I need help to grasp something. 我需要帮助来掌握一些东西。 Currently I am testing the code on microcontroller platform with small amount of memory (ROM(FLASH), RAM). 目前,我正在使用少量内存(ROM(闪存),RAM)在微控制器平台上测试代码。 This is the code 这是代码

void print(const char * c)
{
   printf("%#4x, ",c);
   printf("%s\n",c);
}

This is the function call 这是函数调用

print("Riko");

And the output is: 0x5C4C, Riko 输出为: 0x5C4C,Riko

The memory address 0x5C4C resides in FLASH (MAIN CODE MEMORY) so the literal string "Riko" must also reside in that memory segment? 内存地址0x5C4C驻留在FLASH(主代码内存)中,因此文字字符串"Riko"也必须驻留在该内存段中吗? My question is: When we pass "Riko" as argument to the function print , does it actually mean that we pass the address of the first character of the string "Riko" or ((const char*) 0x5C4C) as argument to the function print ? 我的问题是:当我们将"Riko"作为参数传递给函数print ,实际上是否意味着我们将字符串"Riko"的第一个字符的地址或((const char*) 0x5C4C)作为函数的参数传递给我们。 print Thanks a lot... 非常感谢...

When we pass "Riko" as argument to the function print, does it actually mean that we pass the address of the first character of the string "Riko" 当我们将“ Riko”作为参数传递给函数print时,实际上是否意味着我们传递了字符串“ Riko”的第一个字符的地址

Yes, it means that, however, for printing address you should use: 是的,这意味着,但是,对于打印地址,您应该使用:

   printf("%p", (void*)c);

Yes. 是。 According to section 6.3.2.1, paragraph 3 of the C standard ... 根据C标准6.3.2.1第3段 ...

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. 除非它是sizeof运算符,_Alignof运算符或一元&运算符的操作数,或者是用于初始化数组的字符串文字,否则将类型为“ array of type”的表达式转换为带有键入“指向类型的指针”,它指向数组对象的初始元素,而不是左值。

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

相关问题 修改作为函数传递的字符串文字 - Modifying string literal passed in as a function 函数以整数作为参数时如何传递字符串作为参数? - How to pass a string as an argument when the function has integer as parameter? 如何通过c中的字符串连接创建一个字符串文字作为函数参数 - how to create a string literal as a function argument via string concatenation in c 当您在函数中将文字字符串作为参数传递时,C 是否添加了 '\\0'? - Does C add '\0' when you pass a literal string as an argument in a function? 如何从传递给 C function 的参数(char *)中获取字符串? - How to get string from argument(char*) passed to C function? 将字符串文字作为定义为指针的函数参数传递 - Passing a string literal as a function parameter defined as a pointer 当指向结构的指针作为参数传递给函数时如何填充结构 - How to fill a structure when a pointer to it, is passed as an argument to a function 当数组作为函数的参数传递时,如何找出数组的大小? - How to find out the size of an array when it is passed as an argument for the function? 如何传递文字数组作为函数的输入参数? - How to pass literal array as input argument of the function? 传递给函数调用的字符串文字在哪里存储在c中 - where does string literal passed to function call gets stored in c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM