我正在尝试制作字符串数组,我有rLine
从标准输入读取行的函数,我需要将每个输入的行保存在数组中,但我不知道输入的字符串行的数量。所以我需要动态增加数组大小来存储它们,我写了这样的代码:
char *res[2], *old = res;
while( 1 ){
line = rLine( stdin ), len = strlen( line );
res[row] = (char*)malloc( len + 1);
strcpy( res[row++], line);
res = (char**) realloc( res, row ); /* adding 1 more row, not sure adding size row? */
if ( /*some cond*/ ) break;
}
但是这段代码似乎不起作用,如何正确声明数组并增加它的大小?