Passing variable number of arguments to functions..

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
nishant
New poster
Posts: 1
Joined: Thu Jul 28, 2005 3:10 pm
Location: India
Contact:

Passing variable number of arguments to functions..

Post 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
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

man va_arg
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Post 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
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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));
}

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post 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.
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post 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".
a.souini
New poster
Posts: 3
Joined: Fri Sep 08, 2006 10:45 am
Location: casablanca , morocco

Post 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 .
Post Reply

Return to “C”