gpt4 book ai didi

c - 在 C 中使用 Realloc 的字符串

转载 作者:太空狗 更新时间:2023-10-29 15:36:19 26 4
gpt4 key购买 nike

<分区>

我正在尝试使用 realloc 函数使数组随着用户输入的名称而变大。当我添加 5.element 时它给我一个错误,错误如下:* glibc 检测到 ./a.out: realloc(): invalid next size: 0x00000000017d2010 **代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
char **mtn = NULL;
char x[30];
int i = 0;

while ( strcmp(gets(x), "finish") ){
mtn = realloc( mtn, i*sizeof(char) );
// mtn[i] = realloc( mtn[i], sizeof(x) ); // tried but didnt work
mtn[i] = x;
i++;
}
puts(mtn[1]);

return 0;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com