【问题标题】:Insert element into dynamic char array in C programming在 C 编程中将元素插入到动态 char 数组中
【发布时间】:2016-01-08 06:17:06
【问题描述】:

在 C 编程中尝试将元素添加到动态 char 数组时遇到了一些问题。这是预期的输出:

How many characters do you want to input: 5
Input the string:datas
The string is: datas
Do you want to 1-insert or 2-remove or 3-quit?: 1
What is the character you want to insert: a
Resulting string: adata

我已经在 main 函数中完成了那些用户输入部分,这是 main 中的代码,我在其中接收字符串输入、大小并将它们传递给 insert():

printf("How many characters do you want to input: ");
scanf("%d", &n);
str = malloc(n + 1);
printf("Input the string class: ");
scanf("%s", str);

case '1':
    printf("What is the character you want to insert: ");
    scanf(" %c", &input);
    insert(str, input, n);
    break;

还有我的插入()的部分:

void insert(char *str, char input, int n) {
int i;
size_t space = 1;
for (i = 0; i < n; i++) {
    str[i] = (char)(input + i);
    space++;                       
    str = realloc(str, space); 
    if (i > 2) {
        break;
    }
}

for (i = 0; i < n; i++) {
    printf("%c", str[i]);
}
}

但是,当我尝试从 insert() 中打印出字符串时,假设我输入 'a' 以附加到大小为 5 的动态数组的第一个元素,我得到的结果是 abcd=

我引用了stackoverflow thread,但我不确定如何解决这个问题。提前致谢。

【问题讨论】:

  • 为什么人们不检查scanf的返回值?
  • 抱歉,您的意思是检查作为参数从 main 传递到 insert() 的输入吗?我检查了它,它是正确的。例如,当我输入“b”时,输出变为 bcde=。那么我们说q,outout变成了qrst= etc etc
  • 函数scanf返回一个值——它有多成功。这应该被检查以确保相关变量已被“填充”。人们会打错字,也许会给他们重新输入数据的机会
  • (请阅读scanf - linux.die.net/man/3/scanf的手册页
  • 为什么你不断地将input+i 存储到str 缓冲区中?既然inputa,那么你当然会得到b (a+1)、c (a+2) 等等。为什么不断地realloc?您确切地知道最终字符串需要多长时间。只需malloc 一次即可完成。

标签: c arrays dynamic char


【解决方案1】:

这是代码 - 调用者执行免费位的合同!调用者用insert(&amp;str, input, n)调用它

void insert(char **str, char input, int n) {

char* temp = *str;
int i;

*str = realloc(*str, n + 2); /* realloc first */

if(!*str) /* realloc failed */
{
    fputs("realloc failed", stderr);
    free(temp); /* Free the previously malloc-ed memory */
    exit(-1); /* Exit the program */
}

for (i = n; i >= 0; i--) {
    (*str)[i + 1] = (*str)[i]; /* Move all characters up */ 
}

(*str)[0] = input; /* Insert the new character */

printf("%s", *str); /* Print the new string */
}

对格式问题感到抱歉。那是留给读者的。我没有检查算法,但这不会泄漏内存

【讨论】:

  • 非常感谢!它有效,也感谢您的解释!
  • 但是简短的问题,当你重新分配 *str 时,n 增加了 2。所以我可以假设 1 个空间用于新输入,而另一个用于 '\0'?
  • 假设空字符已经存在。我们只需要改变它。所以 +2 可能是 +1
  • 我明白了。非常感谢您的帮助!
【解决方案2】:

你可以使用

void insert(char **str, char input, int n) {

    char* temp = *str;
    int i;

    *str = realloc(*str, n + 2); /* realloc first */

    if(!(*str)) /* realloc failed */
    {
        fputs("realloc failed", stderr);
        free(temp); /* Free the previously malloc-ed memory */
        exit(-1); /* Exit the program */
    }

    for (i = n; i >= 0; i--) {
        (*str)[i + 1] = (*str)[i]; /* Move all characters up */ 
    }

    **str = input; /* Insert the new character */

    printf("%s", *str); /* Print the new string */
}

并通过引用传递str

insert(&str, input, n); /* Note the '&' */

【讨论】:

  • 这有内存泄漏
  • 是的。修复了它,也是一个错误。谢谢。
  • 不太固定 - realloc 可以返回一个被丢弃的不同地址
  • @CoolGuy No. realloc() 无法神奇地更新所有指向 str 的指针。
  • 感谢您复制我的答案
猜你喜欢
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2017-11-06
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
相关资源
最近更新 更多