Seb*_*Nag 2 c string pointers function char
鉴于源代码 strcpy()
char * strcpy(char *s1, const char *s2)
{
char *s = s1;
while ((*s++ = *s2++) != 0);
return (s1);
}
Run Code Online (Sandbox Code Playgroud)
为什么移交第二个参数有效,以及它如何在内存中查找,因为我没有传递指向函数的指针
char dest[100];
strcpy(dest, "HelloWorld");
Run Code Online (Sandbox Code Playgroud)
这有效,因为,
对于dest,数组作为函数参数传递时,会衰减到第一个元素的地址.那是一个指针.
所以,像一个电话
strcpy(dest, "HelloWorld");
Run Code Online (Sandbox Code Playgroud)
是相同的
strcpy(&dest[0], "HelloWorld");
Run Code Online (Sandbox Code Playgroud)对于"HelloWorld"字符串文字,其类型为char[].所以,它基本上为你提供了第一个元素的地址.