Thread: Function to store command line arguments in array

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    2

    Function to store command line arguments in array

    Hey everyone, I am new to C and I have a simple question regarding a homework assignment I received.

    In class, we have learned how to set up the main method so we can convert command line arguments to integers and utilize them. Here is an example of a method we learned to store command line arguments as integers in an array:

    Code:
    int main (int argc, constchar* argv[]) {int i; 
    int a[argc - 1];
    
    
    // Fill the array with the arguments on the command line
      for (i = 0; i < argc - 1; i++){
             a[i] = atoi(argv[i + 1]);
      }
    
    
    // Now print it out
      print_int_array(a, argc - 1);
    
    
    return0; // Success
    }
    I understand that there are probably better ways to do it than using the atoi function, but for our purposes, the professor said this was okay.

    Now, I am being asked to create a separate function to perform the same task (store integers in an array). I was told I would need to declare the array "out in the main program", and that the function would need to take at least 2 arguments: an array, and an array size.

    I am slightly confused as to go about this. Firstly, what should my return value be? My professor gave a hint that it would be important for me to figure out what the return value is, but I don't see why it would be anything else than void.

    Also, wouldn't I need a third parameter? Perhaps the integers that are being stored in the array? (But how would I represent this as a parameter)?

    Any help/advice would be greatly appreciated! Thanks!

  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
    Perhaps something along the lines of
    store_int_array(a, argv+1, argc - 1);
    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
    Registered User
    Join Date
    Feb 2015
    Posts
    2
    Thanks, that makes sense. I didn't realize argc and argv could be passed as parameters! Problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C++] Command Line Arguments help
    By camelCase in forum C++ Programming
    Replies: 10
    Last Post: 07-03-2010, 08:18 AM
  2. Command line arguments
    By avgprogamerjoe in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2008, 03:21 AM
  3. Command Line Arguments
    By Lang in forum C Programming
    Replies: 8
    Last Post: 03-29-2008, 02:54 AM
  4. command line arguments
    By n00bie in forum C Programming
    Replies: 2
    Last Post: 08-15-2007, 09:15 AM
  5. command line arguments
    By n00bie in forum C Programming
    Replies: 6
    Last Post: 08-15-2007, 08:13 AM

Tags for this Thread