[英]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.