Dynamical array of strings in C
- by Ir0nm
I'm trying to make array of strings, I have function rLine which reads line from stdin, each inputted line I need to save in array, but I don't have any idea about number of inputted string lines. So I need to dynamically increase array size to store them, I wrote such code:
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;
}
But this code doesn't seem to work, how correctly declare array and increase it size?