Fetch the repository succeeded.
#include <stdio.h>
#include <stdlib.h>
char* get_string(void)
{
int size = 0; // number of characters
int capacity = 16; // available capacity, initially 16
char* str = (char*)malloc(capacity); // pointer to the data
if (str == NULL)
{
fprintf(stderr, "malloc failed!\n");
exit(EXIT_FAILURE);
}
int ch;
while ((ch = getchar()) != '\n')
{
if (size + 1 == capacity) // need to expand capacity, be careful '\0'
{
capacity *= 2;
str = (char*)realloc(str, capacity);
if (str == NULL)
{
fprintf(stderr, "malloc failed!\n");
exit(EXIT_FAILURE);
}
}
str[size++] = ch; // append a char
}
str[size] = '\0';
return str;
}
int main(void)
{
printf("Arbitrary length string input function.\n");
while (1)
{
printf(">>> ");
char* str = get_string();
printf("input=\"%s\"\n\n", str);
free(str);
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。