Re: dynamic array of ints

From: Robert Strandh (strandh_at_labri.fr)
Date: 07/28/04


Date: 28 Jul 2004 21:29:17 +0200


"John Hanley" <jdhanley@telusplanet.net> writes:

> I have an interesting problem and I'm not sure the best way to do this.
>
> I am reading in a variable number of integers from a file (eg. 001 002 003
> 004 012 015). At compile time I have no idea how many will be there. But I
> need to parse them out and put them in an array.
>
> I could do an fscanf and just read until '\n', however I won't know how many
> are there and thus don't know how large to make my array.
>
> I could read it in as one large string, traverse through and count the
> number of ints and then allocate memory based on that. However I am not
> sure what to use to read the ints from the string. sscanf works but
> multiple calls to it start at the beginning of the string each time. And
> it seems like a sloppy way to do things anyway.
>
> Are there any suggestions as to what approach to take with this? I wanted
> to avoid using a link list as it seems like alot of work for maybe less than
> 10 ints I need to parse out.
>
> Suggestions? Ideas?

You could do it recursively. Code not tested:

int *
read_ints(FILE *stream, int n, int *how_many)
{
  if(end_of_line(stream))
    {
      *how_many = n;
      return malloc(n * sizeof(int));
    }
  else
    {
      int number = read_int(stream);
      int *result = read_ints(stream, n + 1, how_many);
      result[n] = number;
      return result;
    }
}

...

int
main(void)
{
  FILE *stream = ...;
  int how_many;
  int *numbers = read_ints(stream, 0, &how_many);
  ...
}

-- 
Robert Strandh
---------------------------------------------------------------------
Greenspun's Tenth Rule of Programming: any sufficiently complicated C
or Fortran program contains an ad hoc informally-specified bug-ridden
slow implementation of half of Common Lisp.
---------------------------------------------------------------------


Relevant Pages

  • Re: passing char arrays by reference
    ... I want to pass an array of strings to a function: ... int stuff{ ... stuff is an incompatible pointer type. ... string type; a "string" is a data format, ...
    (comp.lang.c)
  • Re: String of numbers into to array of numbers
    ... then read that string and have the string show the ... individual numbers as in an array. ... int arryCnt = 0; ... qStr= cStr.substring); ...
    (comp.lang.java.help)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: How to Return an array of strings in C (char**)
    ... int start=0; ... // to know the number of elements in the array ... The reason is that you fail to increment str. ... The you could have printed "here" before entering the main loop to see if you actually got there, then str + start to see where you go to in the string on each pass. ...
    (comp.lang.c)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)

Loading