英文:
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
void get_input(char *ptr)
{
ptr = calloc(50, sizeof &ptr);
printf("Type something: ");
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, "Unable to allocate array\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));
}
}
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("Type something: ");
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, "Unable to allocate array\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));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论