My question is based of a previous question asking how C optget works with multiple values: C getopt multiple value
In my case, I have only one argument -i
, which is optional. Users must use this syntax:
/a.out -i file1 -i file2 -i file3
If users do not provide the -i
flag, the program runs fine. Users may provide an unlimited number of files as optional arguments, e.g.
/a.out -i file1 -i file2 -i file3 -i file4 -i file5 ...
I begin with this getopt()
while statement in main()
:
char *input; // ?? Now syntactically correct, but uninitialized?
while ((opt = getopt(argc, argv, "i:"))!= -1){
case 'i':
if (optarg == NULL){
input = NULL;
}
else{
strcpy(input, optarg);
break;
...
}
I then would pass these optional arguments to a function:
function1(char *required_arg, ...)
In the case of the above, it would be:
function1(required_arg, file1, file2, file3, file4, file5)
At the moment, I am defining input
to be the "file". My question is, how do I keep track of an arbitrary number of optional arguments to later pass into a function? The above code is wrong, as I'm redefining input
for each -i
argument passed.
What data structure does one use?