Thread: Reading an unknown # of ints into array

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    Reading an unknown # of ints into array

    Sorry if this is a really simple solution, but it's been a while since I've had a class in C. I came across a question about performing some function (let's say summing) a series of integers input from the command line, but the number of integers is up to the user. How do I read in an indeterminant number of values into an array? I've tried using malloc(argc), but the compiler didn't like it. What am I missing? Thanks for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Rough guess, but you don't need an array to store the integers if all you're going to do is sum them
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are various ways. Maybe a link list would be a good one for this:
    http://cslibrary.stanford.edu/103/

    Or you could use a dynamically sized array (via malloc) and then realloc it when you run out of space.
    http://www.rt.com/man/realloc.3.html

    Whatever, to answer your question on malloc, this will get you enough space to hold a certain number of ints, and you can access it like an array.
    Code:
    #include <stdlib.h>
    
    int *i;
    i = malloc (sizeof(*i) * NUMBER_REQUIRED);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If all the numbers are on the command line, their representations are already stored, so you could try something similar to this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
       if(argc > 1)
       {
          int i, sum = 0;
          for(i = 1; i < argc; ++i)
          {
             sum += atoi(argv[i]);
          }
          printf("sum = %d\n", sum);
       }
       return 0;
    }
    
    /* my output
    C:\Test>test 1 2 3 4
    sum = 10
    
    C:\Test>test 1 2 3
    sum = 6
    
    C:\Test>test 1 2 3 4 5
    sum = 15
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Reading a file and storing it as a 2d Array.
    By MetallicaX in forum C Programming
    Replies: 2
    Last Post: 03-08-2009, 07:33 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. reading through an array
    By ammochck21 in forum C++ Programming
    Replies: 11
    Last Post: 11-29-2006, 07:02 AM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM