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

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

Why are my char arrays returning a null value?

问题

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. void get_input(char *ptr)
  5. {
  6. ptr = calloc(50, sizeof &ptr);
  7. printf("请输入内容:");
  8. fgets(ptr, 50, stdin);
  9. int len = strlen(ptr);
  10. if (*(ptr + len - 1) == '\n')
  11. *(ptr + len - 1) = 0;
  12. printf("%s\n", ptr);
  13. }
  14. int main()
  15. {
  16. char **string_array = calloc(5, sizeof(char *));
  17. if (string_array == NULL)
  18. {
  19. fprintf(stderr, "无法分配数组\n");
  20. exit(1);
  21. }
  22. for (size_t index = 0; index < 5; index++)
  23. {
  24. get_input(string_array[index]);
  25. }
  26. for (size_t index = 0; index < 5; index++)
  27. {
  28. printf("%s\n", *(string_array + index));
  29. }
  30. }

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

英文:

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:

  1. #include &lt;stdio.h&gt;
  2. #include &lt;string.h&gt;
  3. #include &lt;stdlib.h&gt;
  4. void get_input(char *ptr)
  5. {
  6. ptr = calloc(50, sizeof &amp;ptr);
  7. printf(&quot;Type something: &quot;);
  8. fgets(ptr, 50, stdin);
  9. int len = strlen(ptr);
  10. if (*(ptr + len - 1) == &#39;\n&#39;)
  11. *(ptr + len - 1) = 0;
  12. printf(&quot;%s\n&quot;, ptr);
  13. }
  14. int main()
  15. {
  16. char **string_array = calloc(5, sizeof(char *));
  17. if (string_array == NULL)
  18. {
  19. fprintf(stderr, &quot;Unable to allocate array\n&quot;);
  20. exit(1);
  21. }
  22. for (size_t index = 0; index &lt; 5; index++)
  23. {
  24. get_input(string_array[index]);
  25. }
  26. for (size_t index = 0; index &lt; 5; index++)
  27. {
  28. printf(&quot;%s\n&quot;, *(string_array + index));
  29. }
  30. }

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

答案1

得分: 1

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

  1. void get_input(char **ptr)
  2. {
  3. *ptr = calloc(50, sizeof **ptr);
  4. printf("请输入一些内容:");
  5. fgets(*ptr, 50, stdin);
  6. int len = strlen(*ptr);
  7. if ((*ptr)[len - 1] == '\n')
  8. (*ptr)[len - 1] = 0;
  9. printf("%s\n", *ptr);
  10. }
  11. int main()
  12. {
  13. char **string_array = calloc(5, sizeof(char *));
  14. if (string_array == NULL)
  15. {
  16. fprintf(stderr, "无法分配数组\n");
  17. exit(1);
  18. }
  19. for (size_t index = 0; index < 5; index++)
  20. {
  21. get_input(&string_array[index]);
  22. }
  23. for (size_t index = 0; index < 5; index++)
  24. {
  25. printf("%s\n", *(string_array + index));
  26. }
  27. }

你提供的代码看起来是一个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:

  1. void get_input(char **ptr)
  2. {
  3. *ptr = calloc(50, sizeof *ptr);
  4. printf(&quot;Type something: &quot;);
  5. fgets(*ptr, 50, stdin);
  6. int len = strlen(*ptr);
  7. if ((*ptr)[len - 1] == &#39;\n&#39;)
  8. (*ptr)[len - 1] = 0;
  9. printf(&quot;%s\n&quot;, *ptr);
  10. }
  11. int main()
  12. {
  13. char **string_array = calloc(5, sizeof(char *));
  14. if (string_array == NULL)
  15. {
  16. fprintf(stderr, &quot;Unable to allocate array\n&quot;);
  17. exit(1);
  18. }
  19. for (size_t index = 0; index &lt; 5; index++)
  20. {
  21. get_input(&amp;string_array[index]);
  22. }
  23. for (size_t index = 0; index &lt; 5; index++)
  24. {
  25. printf(&quot;%s\n&quot;, *(string_array + index));
  26. }
  27. }

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:

确定