Thread: argc,argv

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    Question argc,argv

    hi,

    how can i pass arguments like main does? i mean as we declare a function say, Func(int x, int y), this function only takes 2 int arguments but main can pass in argc arguments. can we make a function like main that takes a few arguments?
    please advice. thanks.



    thanatos

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    
    void myfunc(int count, char *args[])
    {
    	int i;
    	for (i = 0; i < count; i++)
    		printf("Arg: %s\n", args[i]);
    }
    
    int main(void)
    {
    	char *arglist[] = {"arg1", "arg2"};
    	int argnum = 2;
    	
    	myfunc(argnum, arglist);
    	return 0;	
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The main function doesn't pass argc arguments but only 2 (argc and argv, or whatever you want to call them).
    The second argument is an array of character pointers/arrays.

    If you want to pass a various number of arguments (like printf) you can use a variable argument list (va_list).
    However this is not standard and differs on each OS/compiler.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster

    (va_list).
    However this is not standard and differs on each OS/compiler.
    va_arg and family are ANSI C.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Hammer

    va_arg and family are ANSI C.
    Still learning every day.....
    Thanks for the info Hammer.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    7

    Talking thanks

    thank you very much.


    thanatos

Popular pages Recent additions subscribe to a feed