strcat() 中指针的返回如何工作

发布于 2024-09-25 10:51:36 字数 655 浏览 5 评论 0原文

大家好,我正在尝试弄清楚 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天白云 2024-10-02 10:51:36

您没有返回局部变量的地址。您将返回局部变量的。由于所讨论的变量是一个指针,因此它的值恰好是一个地址。 str 变量中包含的地址指向参数destination 提供的内存块。

您似乎误解的是:

char* str = destination;

不创建 destination 字符串的副本。它创建一个名为 str 的指针,该指针指向 destination 所指向的同一内存位置。当您使用 str 操作该内存块中的字符时,destination 表示的字符串也会被修改,因为 strdestination 指向内存中完全相同的字符串。这就是它“神奇地更新”参数的方式。

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 argument destination.

What you seem to be misunderstanding is that this:

char* str = destination;

Does not create a copy of the destination string. It creates a pointer called str that points at the same memory location that destination points to. When you use str to manipulate the characters in that memory block, the string represented by destination is also modified, because str and destination point into the exact same string of characters in memory. That's how it "magically updates" the parameter.

对不⑦ 2024-10-02 10:51:36

str 是指向您传入的字符串(目标)的指针,因此您正在从 strcat 函数中修改原始变量 string

指针 str 确实在例程结束时被删除,但不再需要了。

顺便说一句,使用“string”一词作为变量名称会令人困惑,因为许多语言保留 string 作为关键字。

str is a pointer to the string you pass in (destination), so you are modifying your original variable string from within your strcat 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.

安人多梦 2024-10-02 10:51:36

该函数的第一行将 *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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文