Thread: command line arguments

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    command line arguments

    I have rethought variodic functions, is there anyway to mimic the

    Code:
    char *args[] /*main() parameter for command line arguments*/
    but use integers, or my own type instead? pass a function several of my types and iterate through them as if they were in an array?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't change the parm style that is used to pass parms to main(), unless you write your own command processor. With a typical operating system, in calling main(), it's always strings.

    If you write your own command processor, I suspect you could do anything you wanted to.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tempster09 View Post
    I have rethought variodic functions, is there anyway to mimic the
    Of course. I kind of thought this is where you were at, I just didn't want to seem patronizing and say maybe you do not understand C that well yet?

    char *argv[] is a string literal array:
    Code:
    char *example[] = {"one", "two", "three"};
    You shouldn't alter this after declaration tho, so it's usefulness is somewhat limited.

    But consider a dynamic pointer array, char **example.
    Code:
    char **example = malloc(3*sizeof(char*));
    This can be resized. Each member is a pointer:
    Code:
    example[1] = malloc(32);
    Of course, if this were a struct or typedef array, you would just go:
    Code:
    example = malloc(3*sizeof(mystruct*));
    example[1] = malloc(sizeof(mystruct));
    So witness, you have a dynamic array of structs. This is the kind of thing you can put into initialization or allocation/deallocation functions. And:
    Code:
    void some_function(int len, mystruct **array);
    Guess what? If "len" is like argc -- that is, the number of pointers in "array", you now have an adaptable arrangement like argc, argv.

    Hope this is what you were looking for.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    char *argv[] is a string literal array:
    Actually it's not. It's an array of pointers to characters. Which could, be if you wanted, an array of pointers to arrays of char:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char foo[BUFSIZ] = "hello";
        char bar[BUFSIZ] = " ";
        char baz[BUFSIZ] = "world!\n";
        char *aop[3];
        size_t x = 0;
        
        aop[0] = foo;
        aop[1] = bar;
        aop[2] = baz;
        
        for( x = 0; x < 3; x++ )
            printf( "%s", aop[ x ] );
        
        return 0;
    }
    Quote Originally Posted by MK27 View Post
    You shouldn't alter this after declaration tho, so it's usefulness is somewhat limited.
    You can actually modify the arguments in arv. It's allowed by the standard.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM