Page 1 of 1

Passing variable number of arguments to functions..

Posted: Fri Jul 29, 2005 8:49 am
by nishant
Hi,

"main(int argc, char* argv[])"

When you invoke the main function from command line , it gets the total number of arguments passed in the variable argc . This approach doesnt work with user-defined functions.Is it possible in some other way to pass variable number of arguments to a function ? How ?

TIA

Nishant

Posted: Fri Jul 29, 2005 9:27 am
by Krzysztof Duleba
man va_arg

Posted: Fri Jul 29, 2005 11:36 am
by CDiMa
Krzysztof Duleba wrote:man va_arg
You are right, but stdarg macros permit to implement functions that work like printf(...).
Actually main doesn't have a variable number of arguments so the original post has a question not well defined ;)

Ciao!!!

Claudio

Posted: Fri Jul 29, 2005 4:28 pm
by Krzysztof Duleba
There's no other way than va_arg to pass variable number of arguments to a function (unless you don't mind using assembler). Note that the original question was well defined ("is there some other way...").

As for functions that _pretend_ to have variable number of arguments (like main), try this

Code: Select all

#include <stdlib.h>
#include <stdio.h>

int foo(int argc, char ** argv){
        int acc = 0;
        int i;

// main counts from 1, but we count from 0
        for(i = 0; i < argc; ++i)
                acc += atoi(argv[i]);
        return acc;
}

int main(){
        char * tab[] = {"1", "2", "123"};
        printf("%d\n", foo(3, tab));
}


Posted: Mon Aug 01, 2005 6:19 am
by sumankar
Krzysztof Duleba wrote: As for functions that _pretend_ to have variable number of arguments (like main),
AFAIK, main() has no _pretensions_ whatsoever!
The C Standard(C99) defines two prototypes for main, viz.

Code: Select all

int main(void)
int main(int argc, char *argv[])
Of course, a conforming implementation could support others, but even those
rarely (or, should I say, never) have variable number of arguments.

Posted: Mon Aug 01, 2005 9:56 am
by Krzysztof Duleba
AFAIK, main() has no _pretensions_ whatsoever!
From user point of view, programs are started with variable number of arguments. From programmer point of view, all of them are stored in argv[]. This is why I called it "pretending".

Posted: Mon Sep 11, 2006 3:29 am
by a.souini
I agree with Krzysztof Duleba because it's the operation system who counts the arguments and put them in memory and save the pointers in the argv[] array.
as an exmple :

Code: Select all

c:\>test.exe parametre1 parametre2
the operating system ( Ms-dos here / or some free alternative) would do several tasks before calling the program , as of taking the parametres and counting them and puts this number in argv , then puting them in memory and points the argv[] to them , then do some other stuff as loading the program into memory and call the main() funtion with those parametre , so at all there are two and not more (here in this case) so the code that Krzysztof Duleba gived resume a little that , but there is more to that because it's assembly more that it's C and that change the prospective of functions and arguments .