Dynamical array of strings in C

Posted by Ir0nm on Stack Overflow See other posts from Stack Overflow or by Ir0nm
Published on 2012-11-20T22:43:08Z Indexed on 2012/11/20 23:00 UTC
Read the original article Hit count: 169

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c

    Related posts about multidimensional-array