strcat() 中指针的返回如何工作
大家好,我正在尝试弄清楚 strcat() 如何返回指针,因此我尝试实现自己的 strcat() 以了解它是如何工作的。以下是我的 mystrcat() 代码,其工作方式与真正的 strcat() 类似:
char *mystrcat(char *destination, char *source)
{
char *str = destination;
while (*str != '\0')
{
str++;
}
while (*source != '\0')
{
*str = *source;
str++;
source++;
}
*str = '\0';
return str;
}
因此,让我们在我的 main() 中说,我的
char string[BUFSIZ];
mystrcat(string, "hello");
printf("%s\n", string);
输出将
hello
如预期的那样。我不明白的是,返回局部变量 str 的地址如何神奇地使变量 string 指向它,以及为什么变量 str 在函数终止时不会被删除。
Hey guys I'm trying to figure how pointers are returned by strcat(), so I tried implementing my own strcat() to see how it works. The following is my code for mystrcat(), which works like the real strcat():
char *mystrcat(char *destination, char *source)
{
char *str = destination;
while (*str != '\0')
{
str++;
}
while (*source != '\0')
{
*str = *source;
str++;
source++;
}
*str = '\0';
return str;
}
So let's say in my main(), I have
char string[BUFSIZ];
mystrcat(string, "hello");
printf("%s\n", string);
The output would be
hello
as expected. What I don't get is how returning the address of the local variable, str, would magically make the variable, string, point to it and also why is the variable, str, not deleted when the function terminates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有返回局部变量的地址。您将返回局部变量的值。由于所讨论的变量是一个指针,因此它的值恰好是一个地址。
str
变量中包含的地址指向参数destination
提供的内存块。您似乎误解的是:
不创建
destination
字符串的副本。它创建一个名为str
的指针,该指针指向destination
所指向的同一内存位置。当您使用str
操作该内存块中的字符时,destination
表示的字符串也会被修改,因为str
和destination
指向内存中完全相同的字符串。这就是它“神奇地更新”参数的方式。You're not returning the address of the local variable. You're returning the value of the local variable. Since the variable in question is a pointer, its value happens to be an address. The address that is contained in the
str
variable points into the memory block provided by the argumentdestination
.What you seem to be misunderstanding is that this:
Does not create a copy of the
destination
string. It creates a pointer calledstr
that points at the same memory location thatdestination
points to. When you usestr
to manipulate the characters in that memory block, the string represented bydestination
is also modified, becausestr
anddestination
point into the exact same string of characters in memory. That's how it "magically updates" the parameter.str
是指向您传入的字符串(目标)的指针,因此您正在从strcat
函数中修改原始变量string
。指针 str 确实在例程结束时被删除,但不再需要了。
顺便说一句,使用“string”一词作为变量名称会令人困惑,因为许多语言保留
string
作为关键字。str
is a pointer to the string you pass in (destination), so you are modifying your original variablestring
from within yourstrcat
function.The pointer
str
does get deleted at the end of the routine, but it is not needed anymore.BTW, it is confusing to use the word "string" as the name of a variable, because many languages reserve
string
as a keyword.该函数的第一行将 *str 指定为与 *destination 相同。实际上,当您返回时,您将返回 *destination,这与 *str 相同。
没有分配内存,因此不必删除内存。
The first line of the function, you're assigning *str to be the same as *destination. In effect, when you return, you're returning *destination, which is the same as *str.
No memory is allocated, so no memory must be deleted.