为什么我的字符数组返回了一个空值?

huangapple go评论60阅读模式
英文:

Why are my char arrays returning a null value?

问题

我正在尝试编写一个函数,该函数将值读入指针数组以存储不同长度的字符串。这些字符串似乎在get_input中正确存储,但在主函数中打印时具有空值。请参见下面的代码:

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

void get_input(char *ptr)
{
    ptr = calloc(50, sizeof &ptr);
    printf("请输入内容:");
    fgets(ptr, 50, stdin);
    int len = strlen(ptr);
    if (*(ptr + len - 1) == '\n')
        *(ptr + len - 1) = 0;
    printf("%s\n", ptr);
}

int main()
{
    char **string_array = calloc(5, sizeof(char *));
    if (string_array == NULL)
    {
        fprintf(stderr, "无法分配数组\n");
        exit(1);
    }

    for (size_t index = 0; index < 5; index++)
    {
        get_input(string_array[index]);
    }

    for (size_t index = 0; index < 5; index++)
    {
        printf("%s\n", *(string_array + index));
    }
}

我做错了什么?为什么字符串没有正确存储?

英文:

I'm trying to write a function that reads values into a pointer array to store strings of variable lengths. The strings appear to be stored correctly in get_input, but have a null value when printed in the main function. Please see the code below:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

void get_input(char *ptr)
{
    ptr = calloc(50, sizeof &amp;ptr);
    printf(&quot;Type something: &quot;);
    fgets(ptr, 50, stdin);
    int len = strlen(ptr);
    if (*(ptr + len - 1) == &#39;\n&#39;)
        *(ptr + len - 1) = 0;
    printf(&quot;%s\n&quot;, ptr);
}

int main()
{
    char **string_array = calloc(5, sizeof(char *));
    if (string_array == NULL)
    {
        fprintf(stderr, &quot;Unable to allocate array\n&quot;);
        exit(1);
    }

    for (size_t index = 0; index &lt; 5; index++)
    {
        get_input(string_array[index]);
    }

    for (size_t index = 0; index &lt; 5; index++)
    {
        printf(&quot;%s\n&quot;, *(string_array + index));
    }
}

What am I doing wrong? Why aren't the strings correctly stored?

答案1

得分: 1

void get_input(char *ptr) - ptr 是一个局部变量,对它赋值不会改变在调用时传递的对象。你需要使用指向指针的指针:

void get_input(char **ptr)
{
    *ptr = calloc(50, sizeof **ptr);
    printf("请输入一些内容:");
    fgets(*ptr, 50, stdin);
    int len = strlen(*ptr);
    if ((*ptr)[len - 1] == '\n')
        (*ptr)[len - 1] = 0;
    printf("%s\n", *ptr);
}

int main()
{
    char **string_array = calloc(5, sizeof(char *));
    if (string_array == NULL)
    {
        fprintf(stderr, "无法分配数组\n");
        exit(1);
    }

    for (size_t index = 0; index < 5; index++)
    {
        get_input(&string_array[index]);
    }

    for (size_t index = 0; index < 5; index++)
    {
        printf("%s\n", *(string_array + index));
    }
}

你提供的代码看起来是一个C语言程序,它使用指向指针的指针来分配内存并读取用户输入的文本。

英文:

void get_input(char *ptr) - ptr is a local variable and assigning to it does not change the object you pass when you call it. You need to use the pointer to the pointer:

void get_input(char **ptr)
{
    *ptr = calloc(50, sizeof *ptr);
    printf(&quot;Type something: &quot;);
    fgets(*ptr, 50, stdin);
    int len = strlen(*ptr);
    if ((*ptr)[len - 1] == &#39;\n&#39;)
        (*ptr)[len - 1] = 0;
    printf(&quot;%s\n&quot;, *ptr);
}

int main()
{
    char **string_array = calloc(5, sizeof(char *));
    if (string_array == NULL)
    {
        fprintf(stderr, &quot;Unable to allocate array\n&quot;);
        exit(1);
    }

    for (size_t index = 0; index &lt; 5; index++)
    {
        get_input(&amp;string_array[index]);
    }

    for (size_t index = 0; index &lt; 5; index++)
    {
        printf(&quot;%s\n&quot;, *(string_array + index));
    }
}

https://godbolt.org/z/dWdK9jdza

huangapple
  • 本文由 发表于 2023年5月15日 01:42:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定