user1212697
user1212697

Reputation: 699

Passing multiple arguments to main in c

int main(int argc, char *argv[])    // Send the path as a parameter
{
    char ptr2[BUFSIZE]; 
    va_list list;
    va_start(list,argv[]);
}

Let's say I got this code, if I'm getting multiple arguments in this main (say I do something like "./program car house student phone"), each of them would be a different argument, how do I put them on a va_list?

I don't know how many arguments I'm going to receive, but I need to put it on a va_list, help pls! thanks!

Upvotes: 0

Views: 730

Answers (1)

user529758
user529758

Reputation:

There's no portable way to do this. However, you can pass an initialized va_list down to your other function taking a va_list by writing something like

va_list args;
va_start(args, argv); // or maybe argc? Whatever, this doesn't make sense anyway
// read: "please don't litter production code with UB"
call_the_function(foo, args);
va_end(args);

And, by the way:

I don't know how many arguments I'm going to receive.

This is not true - that's what the argc argument of main() is for.

Upvotes: 2

Related Questions