Thread: make array with command line help

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Unhappy make array with command line help

    hi, i am trying to make an array using a command line arguement. I am not good with using command line arguements so that is why i am posting here.

    $prog 10 20

    using the command above, i am supposed to make an array with max size of 10 and another array max size of 20. Can anyone help?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    This should get you started in the right direction.

    Code:
    int main(int argc, char *argv[])
    {
        char *array1, *array2;
        int array1size, array2size;
    
        if(argc != 3)
            return 0;
    
        array1size = atoi(argv[1]);
        array2size = atoi(argv[2]);
    
        printf("array1size = %d\narray2size = %d\n",array1size,array2size);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    thank you very much

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    see the thing is.... what if someone types:

    $prog 10 20 <-- needs to make 2 arrays, first with 10 max size next with 20 max size

    or

    $prog 10 20 30 <-- needs to make 3 arrays, first with 10 max size next with 20 max size and last with 30 max size

    any suggestions?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Then argc would be 4 instead of 3. How about an array of arrays?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    i understand that it would be 4, but say you dont know how many ints are gonna be put after $prog . what if it is different everytime... and everytime it would have to create different amount of arrays.

  7. #7
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Code:
    int i;
    char* arrays[argc];
    for (i = 1; i < argc; i++) {//i = 1 because argv[0] is program name.
        argc[i] = malloc(atoi(argv[i]));
        //check that malloc worked.
    }
    Or something similar.
    -S

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        int arraySize[argc-1];
        int i;
    
        if(argc < 2)
        {
            return 0;
        }
    
        for( i = 1; i < argc; i++ )
        {
            arraySize[i] = atoi(argv[i]);
            printf("arraySize[%d] = %d\n", i, arraySize[i]);
        }
    
        return 0;
    }

    that's going to make you an array of array sizes

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    thats not kinda what im lookin for, heres an example:

    $prog 10 20

    would declare 2 arrays like a[10] and b[20]

    $prog 10 20 25

    would declare 3 arrays like a[10], b[20], and c[25].

    thanks for reminding me about malloc tho, that should be used for sure.

    Here is some info describe what is to be done:

    http://www.sci.brooklyn.cuny.edu/~sd.../Program5.html

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There is no way you're going to be able to have separate arrays. It's just not possible. What you're going to have to do is an array of arrays. You can do this:
    Code:
    int **array;
    int i;
    
    array = malloc(sizeof(int *) * (argc - 1));
    for(i = 1;i < argc;++i)
      array[ i ] = malloc(sizeof(int) * atoi(argv[ i ]));
    BLAM! You're done.

    NOTE: argv[0] is pretty useless for your program so you should start with argv[1].

    EDIT: Reading your assignment it's obvious that you're not even trying to use real arrays in their normal sense. You're using some facsimile of one. Next time be sure to ask the question you really mean to ask because all of the replies you've gotten until now are completely irrelevant to your assignment and you've managed to waste a lot of volunteer free time.

    What you really want is to create an instance of a structure for each command line argument and allocate memory for the data member in each. That's not even close to what you've been asking for in this thread.

    EDIT 2: Why is the tag for italics parsed within code tags? That's just annoying.
    Last edited by itsme86; 12-06-2005 at 05:20 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:12 PM