Reputation: 121
Suppose I have a function with arbitrary argument. Example :
execl(char *path, char *arg0,...,char *argn, 0)
Function definition: execl()
So no matter how many appropriate arguments (>=3) I have sent to it, it will work. Now say I have a array of string which contains arg0
, arg1
, arg2
, ... argn
.
How can I call this function with my arbitrary number of string. That means, if I have a array of string size 3 then I want to call a function like this,
execl(char *path, char *arg0,char *arg1, char *arg2, 0)
and if I have a array of string size 4 then I want to call a function like this
execl(char *path, char *arg0,char *arg1, char *arg2,char *arg4, 0)
Is there any automatic way to do so. I do not want to use if condition to do something like this...(if I have array size 2 call a two argument function, if I have array size 3 call a three argument function, etc). Is there any process? Please explain in a short code.
Upvotes: 1
Views: 154
Reputation: 11728
If I understand you correctly, you are asking how to call a function with a variable number of arguments that you build within a single function. The short answer is you can't. While c has variadic function support, it is to receive not to call. Meaning, you can write a function that accepts a variable number of arguments, but cannot pass them to another https://en.wikipedia.org/wiki/Variadic_function
NOTE: I wasn't sure whether your execl()
code was just meant to demonstrate your question OR you were looking for how to use exec*()
functions with variable arguments. If it was the latter, check out execv()
which accepts an array of strings.
Upvotes: 4