我正在尝试使用 realloc 函数使数组在用户输入名称时变大。当我添加 5. 元素时,它给了我一个错误,错误如下: * glibc detected./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;
}