1

In C we can have variadic functions like:

void abc(int x, .. )
{
}

and we can call them with variable number of arguments like:

abc(1,2,3) or abc(1,2,3,4,5)

But, in both of the above function calls, we have called the variadic function abc with parameters that are already known to us; "1, 2, 3" in the first case and "1, 2, 3, 4, 5" in the second case.

My question is:

Is there any way to call a variadic function with a variable number of arguments inputted by the user during run-time?

For example, in main(), I ask the user to enter a certain number of inputs and then pass those inputs into the variadic function using parameters p1, p2 , p3 and so on...

Anish Ramaswamy
  • 2,326
  • 3
  • 32
  • 63
  • 2
    This sounds like yet another search for a needlessly complex solution, for the sake of making things complicated. You shouldn't even need to use va_lists ever in a C program, they are a superfluous feature of the language. – Lundin Apr 04 '13 at 08:31
  • @Lundin I find `printf` rather nifty. I can imagine that there are other cases where variable argument lists are useful albeit not strictly necessary. – Klas Lindbäck Apr 04 '13 at 08:37
  • @KlasLindbäck While the printf/scanf functions are powerful, they are also incredibly complex, slow, inefficient and very unsafe (no type safety, possible buffer overruns etc). On top of that they deviate syntactically from the rest of the language. Personally, I think they are quite horrible. No matter what you think of these functions, there shouldn't be any need for a programmer to create their own printf functions, so va_lists are quite superfluous. – Lundin Apr 04 '13 at 08:45
  • possible duplicate of [Calling a C function with a varargs argument dynamically](http://stackoverflow.com/questions/280940/calling-a-c-function-with-a-varargs-argument-dynamically) – Hasturkun Apr 04 '13 at 08:56
  • @Lundin, please write `printf(3)` without `va_list` and check back with us. There _are_ uses (I've written some varags functions myself). Not frequent, granted. – vonbrand Apr 04 '13 at 17:59
  • @vonbrand Que? printf(3)? – Lundin Apr 05 '13 at 06:11

5 Answers5

1

You can use array for example:

void abc(int size, int *arr) { }

In the main

int size;
int A[MAX];

size = 3;
A[0] = 1; A[1] = 2; A[2]=3;
abc(size,A);

Example

int size=0;
int A[MAX]; 
while(size<MAX && scanf(" %d",&A[size])!=EOF) {
   size++;
}

 if(size>0) abc(size, A);
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
1

Practically and theoretically its not advisable to invent such things though workarounds are very much possible... Use some good data structures like linked list to store any number of elements and pass the head pointer in the calling function.

Anshul
  • 1,416
  • 1
  • 16
  • 42
0

No, this is not possible.
The compiler needs to generate appropriate code and reserve space for identifiers during compilation. You would try to change a static binary file, which is not possible. This is actually why va_args exist, to be able to pass an unknown amount of parameters.

bash.d
  • 13,029
  • 3
  • 29
  • 42
  • Well you might place the parameters on the stack manually, but that wouldn't be wise i think. – LtWorf Apr 04 '13 at 08:19
  • @LtWorf There might be workarounds, you are right. But they will be very inconvenient! And `va_args` is a nice gadget, so why reinvent the wheel? – bash.d Apr 04 '13 at 08:22
0

It really looks like you should pass a pointer to an array instead.

void yourfunction(int *array, size_t len) {
    //Do what you want in your function
}
LtWorf
  • 7,286
  • 6
  • 31
  • 45
0

You can not do that easily. Variable argument function are compile according to a certain calling convention. So any attempt to dynamically mimic the calling convention will not be portable.

One solution if you really need to do this is to use libffi. There is some support for calling variadic function.

Otherwise I agree with others solution of using an array with there size as parameter.

mathk
  • 7,973
  • 6
  • 45
  • 74