C 每个字符串长度的动态n个字符串可以是随机的(取决于用户输入)

C 每个字符串长度的动态n个字符串可以是随机的(取决于用户输入),c,pointers,C,Pointers,我正在写如何为n个字符串动态分配内存,每个字符串的长度可以是您想要的长度。 所以我知道使用双指针,但不知道如何为每个字符串分配适当的空间。 我的代码: inti,n; 字符**str; printf(“输入n(字符字符串):”; scanf(“%d”和“&n”); str=(char**)malloc(n*sizeof(char*); printf(“逐个输入字符串:\n”); 对于(i=0;i如果您不想浪费空间,只想分配足够的内存来容纳字符串,那么您需要逐个字符地读取,直到读取换行符为止,根据

我正在写如何为n个字符串动态分配内存,每个字符串的长度可以是您想要的长度。 所以我知道使用双指针,但不知道如何为每个字符串分配适当的空间。 我的代码:

inti,n;
字符**str;
printf(“输入n(字符字符串):”;
scanf(“%d”和“&n”);
str=(char**)malloc(n*sizeof(char*);
printf(“逐个输入字符串:\n”);

对于(i=0;i如果您不想浪费空间,只想分配足够的内存来容纳字符串,那么您需要逐个字符地读取,直到读取换行符为止,根据需要重新分配(使用)

也许是这样的:

for (i = 0; i < n; ++i)
{
    size_t current_size = 1;  // Start with space for one character, the null-terminator
    str[i] = malloc(current_size);
    str[i][0] = '\0';  // String null-terminator

    int ch;  // Must be an int for the EOF check to work

    // Read characters one by one, until eof (or error) or newline
    while ((ch = getchar()) != EOF && ch != '\n')
    {
        char *temp_str = realloc(str[i], current_size + 1);  // Increase memory by one character
        if (temp_str == NULL)
        {
            // Error allocating memory, do something useful here
            break;
        }

        str[i] = temp_str;
        str[i][current_size - 1] = ch;  // Add the newly read character
        str[i][current_size] = '\0';  // Add the new null-terminator
        ++current_size;  // Increase the size
    }
}
(i=0;i { size\u t current\u size=1;//以空格开头,表示一个字符,即空终止符 str[i]=malloc(当前_大小); str[i][0]='\0';//字符串空终止符 int ch;//必须是int才能执行EOF检查 //逐个读取字符,直到eof(或错误)或换行 而((ch=getchar())!=EOF&&ch!='\n') { char*temp_str=realloc(str[i],当前大小+1);//将内存增加一个字符 if(temp_str==NULL) { //分配内存时出错,请在此处执行一些有用的操作 打破 } str[i]=temp_str; str[i][current_size-1]=ch;//添加新读取的字符 str[i][current_size]='\0';//添加新的空终止符 ++当前大小;//增加大小 } }
在用户输入字符串之前,您无法神奇地“预测”字符串的长度!但是,您可以分配一个给定最大长度的“工作缓冲区”,读入该缓冲区,然后分配所需的内存并复制该字符串

您可以结合使用
malloc
strlen
strcpy
来实现这一点,但该功能将一下子为您实现所有功能:

#包括
#包括
#包括
#定义MAXLENGTH 5000
int main()
{
inti,n;
字符**str;
字符缓冲区[MAXLENGTH];//用于读取字符串的工作缓冲区
printf(“输入n(字符字符串):”;
scanf(“%d”和“&n”);
str=malloc(n*sizeof(char*));
printf(“逐个输入字符串:\n”);
对于(i=0;i
此外,如注释中所述,
get
函数已从C库中删除:请改用
fgets()
(如图所示)。请参阅:


另外,你不需要强制转换malloc的结果:。

循环条件
我糟糕透了,它应该是
离子另一个注意:永远不要使用
gets
。它是一个旧的函数,甚至已经从C标准中删除了。请使用例如。字符串不能真的是“只要我们想要”。您需要选择:您的程序中有一个最大大小的harcoded,或者您询问用户每个字符串的最大大小是多少。
for(i=0; i<n; i++) {
        gets(str[i]);
        str[i] = (char*)malloc(strlen(str[i])*sizeof(char));
for (i = 0; i < n; ++i)
{
    size_t current_size = 1;  // Start with space for one character, the null-terminator
    str[i] = malloc(current_size);
    str[i][0] = '\0';  // String null-terminator

    int ch;  // Must be an int for the EOF check to work

    // Read characters one by one, until eof (or error) or newline
    while ((ch = getchar()) != EOF && ch != '\n')
    {
        char *temp_str = realloc(str[i], current_size + 1);  // Increase memory by one character
        if (temp_str == NULL)
        {
            // Error allocating memory, do something useful here
            break;
        }

        str[i] = temp_str;
        str[i][current_size - 1] = ch;  // Add the newly read character
        str[i][current_size] = '\0';  // Add the new null-terminator
        ++current_size;  // Increase the size
    }
}