返回局部变量指针 - C [英] Returning Local Variable Pointers - C

查看:62
本文介绍了返回局部变量指针 - C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C 编写一个函数来返回传递给函数的字符串的反向.

I'm trying to write a function in C to return the reverse of the a string that is passed into to the function.

一旦传入字符串,我就声明一个本地 char [] 来填充字符串的反向.

Once the string is passed in I declare a local char [] to fill with the reverse of the string.

但是,当我编译程序时,我收到一条警告,说明 warning: function returns address of local variable return test;

However, when I compile the program I receive a warning stating warning: function returns address of local variable return test;

我可以从 C 中的函数返回局部变量指针吗?

Am I allowed to return local variable pointers from functions in C?

char *reverseString(char *str)
{
    int i, j;
    char test[strlen(str)];

    if(str == 0)
        return;

    for(i = 0, j = strlen(str); i < strlen(str), j >= 0; i++, j--)
        str[j] = test[i];

    return test;
}

推荐答案

  1. 字符串需要更多空间,strlen() 给出字符数,但是c 字符串使用一个额外的字符来标记字符串的结尾,'\0' ascii nul 字符,所以 1 + strlen(otherString) 总是需要复制一个字符串.

  1. You need more space for a string, strlen() gives you the number of characters, but c strings use an extra character to mark the end of the string, the '\0' ascii nul character, so 1 + strlen(otherString) is always required to copy a string.

在取消引用后检查 str 是否为 NULL.这没有意义,首先检查 NULL,然后调用 strlen().

You check str for NULL after dereferencing it. That doesn't make sense, fist check for NULL and then call strlen().

使用 strlen() 这种方式不是一个好主意,因为在 c 中字符串长度没有存储在任何地方,调用 strlen() 计算字符串基本上这样

Using strlen() that way is not a good idea, because in c the string length is not stored anywhere, calling strlen() computes the string basically this way

size_t strlen(const char *string)
{
    size_t count = 0;
    while (*string++ != '\0')
        count++;
    return count;
}

在那里你可以看到终止 '\0' 的重要性,以及为什么你不应该在循环中调用 strlen() 一个长度不长的字符串't 改变.

there you can see how important the terminating '\0' is, and why you should not call strlen() in a loop for a string whose length doesn't change.

如果你返回的是局部变量的地址,它不会像你想象的那样工作,因为函数返回时它会被释放,所以你需要使用动态分配,因为存在malloc() 这使得它很容易做到,所以在你的情况下它会是这样的

If you return the address of a local variable, it will not work as you expect, because it wil be deallocated when the function returns, so you need to use dynamic allocation, for that there exists malloc() which makes it easy to do, so in your case it would be something like this

char *reverseString(char *str)
{
    size_t index;
    char  *test;
    size_t length;

    if (str == NULL)
        return NULL;

    length = strlen(str);
    test   = malloc(1 + length);
    if (test == NULL)
        return NULL;
    /* perform string reversal */
    for (index = 0 ; index < length ; index++)
        test[index] = str[length - index - 1];
    /* add the `nul` terminator */
    test[length] = '\0';
    /* return the newly allocated and initialized data */
    return test;
}

这篇关于返回局部变量指针 - C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆