简体   繁体   English

如何修改在 function 中作为参数传递的 char 指针?

[英]How to modify a char pointer passed as parameter in a function?

So, I am doing my own rudimentary version of itoa() , and I just realized I don't exactly know how to modify a char* passed as parameter, or if there is something wrong with the way I am doing it...所以,我正在做我自己的itoa()的基本版本,我刚刚意识到我并不完全知道如何修改作为参数传递的char* ,或者我这样做的方式是否有问题......

The way I am doing it is by doing malloc() onto the buffer passed as argument, then write the number into it.我这样做的方式是在作为参数传递的缓冲区上执行malloc() ,然后将数字写入其中。 This is apparently working before returning from the function (the buffer is printed correctly), but then when trying to print it back in main() , it segfaults.这显然在从 function 返回之前有效(缓冲区打印正确),但是当尝试在main()中打印回来时,它会出现段错误。

If I understand the error correctly, I am changing the address buff points to inside with that malloc() , and then modify its contents, but the new malloc'd address inside is not returned.如果我正确理解错误,我将使用该malloc()将地址buff指向内部,然后修改其内容,但不会返回内部新的malloc'd地址。 How could I do that without changing the parameters or the return value?我怎么能在不改变参数或返回值的情况下做到这一点?

int itoa(int i, char *buff) {
    int length = 0;
    
    // get the length
    long temp = 1;
    while (temp <= i) {
        length++;
        temp *= 10;
    }

    buff = malloc(length + 1); // buff will have 'length' chars + one extra (\0)

    int j = 0;
    do {                            /* generate digits in reverse order */
        buff[j++] = i % 10 + '0';   /* get next digit */
    } while ((i /= 10) > 0);        /* delete it */

    buff[length] = '\0';
    
    // reverse it
    int k, l;
    char c;
    for (k = 0, l = length - 1; k<l; k++, l--) {
        c = buff[k];
        buff[k] = buff[l];
        buff[l] = c;
    }

    printf("buff's now:%s\n", buff);

    return 0;
}

int main() {
    char *buff = NULL;
    itoa(45, buff);

    printf("%s\n", buff);
}

Your pointer isn't modified as it was copied.您的指针在复制时没有被修改。 You can read more here .你可以在这里阅读更多。 You can try this code after reading the above link.阅读上述链接后,您可以尝试此代码。

#include <stdlib.h>
#include <stdio.h>

int itoa_(int i, char **parabuff)
{
    int length = 0;
    // get the length
    long temp = 1;
    while (temp <= i)
    {
        length++;
        temp *= 10;
    }

    char *buff = malloc(length + 1); // buff will have 'length' chars + one extra (\0)

    int j = 0;
    do
    {                             /* generate digits in reverse order */
        buff[j++] = i % 10 + '0'; /* get next digit */
    } while ((i /= 10) > 0);      /* delete it */

    buff[length] = '\0';

    // reverse it
    int k, l;
    char c;
    for (k = 0, l = length - 1; k < l; k++, l--)
    {
        c = buff[k];
        buff[k] = buff[l];
        buff[l] = c;
    }

    printf("buff's now: %s\n", buff);

    *parabuff = buff;

    return 0;
}

int main()
{
    char *buff = NULL;
    itoa_(45, &buff);

    printf("buff in main: %s\n", buff);
}

//OUTPUT
buff's now: 45
buff in main: 45

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM