帮忙写getstring函数

发布于 2024-08-28 17:50:53 字数 523 浏览 10 评论 0原文

我在编写 getstring 函数时遇到了一些麻烦,这就是我到目前为止所遇到的。

问候, V

const char* getstring()
{


    char *buffer;
    int i = 255;

    buffer = (char *)malloc(i*sizeof(char));

    *buffer = getchar();
    while ( *buffer != '\n' )
    {
        buffer++;
        *buffer = getchar();
    }
    *buffer = '\0';

    const char* _temp = buffer;
    return _temp;
}


int main()
{
    char* temp = getstring();

    for ( ;temp++ ; *temp != '\0')
    {
        printf("%c", *temp);
    }

    return 0;
}

Im having some trouble writing a getstring function, this is what I have so far.

Regards,
V

const char* getstring()
{


    char *buffer;
    int i = 255;

    buffer = (char *)malloc(i*sizeof(char));

    *buffer = getchar();
    while ( *buffer != '\n' )
    {
        buffer++;
        *buffer = getchar();
    }
    *buffer = '\0';

    const char* _temp = buffer;
    return _temp;
}


int main()
{
    char* temp = getstring();

    for ( ;temp++ ; *temp != '\0')
    {
        printf("%c", *temp);
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

裸钻 2024-09-04 17:50:53

_temp 指向字符串的终止 '\0' 时,您将 _temp 设置为 buffer

将行:移动

const char* _temp = buffer;

到紧接行:之后,

buffer = (char *)malloc(i*sizeof(char));

以便 _temp 指向缓冲区的开头。

您还有其他一些问题:

  1. 不要使用名称_temp - 带有前导下划线的名称被保留;

  2. 您需要测试您向缓冲区写入的字节数不超过 i 个字节;

  3. 您应该测试 malloc() 返回 NULL;

  4. 您需要测试 getchar() 返回 EOF。这意味着您需要将 getchar() 结果存储在 int 类型的变量中,然后再将其分配给 *buffer

  5. 正如 Michael Mrozek 在评论中指出的那样,for 循环中的表达式是错误的。

...并且从风格上来说,sizeof(char) 始终为 1,因此无需乘以它;并且在 C 中强制转换 malloc() 的结果是不必要的,并且被认为是不可取的(与 C++ 不同,C++ 是必需的)。

You're setting _temp to buffer when the latter points at the terminating '\0' of the string.

Move the line:

const char* _temp = buffer;

to be immediately after the line:

buffer = (char *)malloc(i*sizeof(char));

so that _temp is pointing to the start of the buffer.

You have some other problems:

  1. Don't use the name _temp - names with a leading underscore are reserved;

  2. You need to test that you don't write more than i bytes into the buffer;

  3. You should test for malloc() returning NULL;

  4. You need to test for getchar() returning EOF. This will mean you need to store the getchar() result in a variable of type int before you assign it to *buffer;

  5. As Michael Mrozek points out in a comment, the expressions in your for loop are the wrong way around.

...and as a point of style, sizeof(char) is always 1, so multiplying by it is unnecessary; and casting the result of malloc() is unnecessary in C and considered undesirable (unlike C++, where it is required).

澜川若宁 2024-09-04 17:50:53

为什么不直接使用

char buffer[255];
scanf("%254s", &buffer);

char* buffer = readline("GO GO GO:");

Why not just use

char buffer[255];
scanf("%254s", &buffer);

or

char* buffer = readline("GO GO GO:");
爱*していゐ 2024-09-04 17:50:53
const char* _temp = buffer;

将上述语句移至 malloc 调用之后

重要:
main() 使用后释放分配给 buffer 的内存。

free(temp);
const char* _temp = buffer;

Move the above statement just after the call to malloc

Important:
Free the memory allocated to buffer after its use in main().

free(temp);
知足的幸福 2024-09-04 17:50:53

您需要在调用 malloc() 之后立即跟踪分配的指针 - 由 malloc() 返回的值,以便您可以将其传递回调用者。您还应该检查 EOF 以及换行符 - 这需要一个 int (而不是 char)来保存 getchar() 中的值。至少!

You need to keep track of the allocated pointer - the value returned by malloc() - immediately after calling malloc() so you can pass it back to the caller. You should also check for EOF as well as newline - and that requires an int (not a char) to hold the value from getchar(). At minimum!

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