Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Handling Variable arguments in C (http://www.go4expert.com/forums/handling-variable-arguments-c-t29285/)

IndiraP 10Nov2012 19:58

Handling Variable arguments in C
 
Code:

#include<stdio.h>
#include<stdarg.h>
void func(int n,...);
void main()
{
  func(3,1,2,3);
}
void func(int n,...)
{
  int i,t,sum=0;
  va_list li;
  va_start(li,n);
 for(i=0;i<n;i++)
{
    t=va_arg(li,int);
    sum+=t;
}
printf("Sum = %d",sum);
va_end(li);
}

This program outputs 6...
va_list is lyk datatype to handle variable arguments..
va_start takes the variable of va_list and the no. of variable arguments u r going to enter(i.e., nothing but the first argument in the func declaration)... and va_start points to the first of the variable arguments..
va_arg takes that variable of va_list and the return type that is expected..
At the end , va_end clears the list..
Note: No. of variable arguments u r going to enter must be mentioned in the definition and
there should be atleast one argument defined before the variable arguments and the ... should at the end of the argument list.. and the variable of va_list ,the same should be used for the other va_xxxx functions..

i hope i have mentioned everything right.. :charming:
but i have a problem to be cleared...that is..
this works if all the variable arguments are of same type..wat if i want each one of the variable argument be of different data type.. :worried: ???

xpi0t0s 11Nov2012 14:45

Re: Handling Variable arguments in C
 
In that case you need some way to work out what the types are. The calling convention in C means that the calling function places the items on the stack then calls the function, so one way is to do what printf does and to match the %-codes with the parameters. Another way might be for the parameters to be delivered in pairs where one indicates the type and the other the value, for example
Code:

func(2, TYPE_INT,1, TYPE_PCHAR,"Hello");

IndiraP 11Nov2012 20:09

Re: Handling Variable arguments in C
 
if i ask the user his preferred data type how can i tell it to the compiler about it??
i want to do this because i want function take arguments at the run time from the user watever his choise may be..!!!
i mean

printf("number/decimal/alphabets");
scanf(....);

how can i read it???

xpi0t0s 13Nov2012 04:56

Re: Handling Variable arguments in C
 
Basically you would decide what types you want to support then prompt the user first which type, then which value, and you could use a switch to perform the input:
Code:

printf("Enter type (1=int, 2=char) : ");
fgets(buf,30,stdin);
utype=atoi(buf);
printf("Enter value : ");
fgets(buf,30,stdin);
switch (utype)
{
case 1: // int
  ival=atoi(buf);
  break;
case 2: // char*
  strcpy(sval,buf);
  break;
}
printf("You entered a");
switch(utype)
{
case 1: // int
  printf("n integer, value '%d'\n",ival);
  break;
case 2: // char*
  printf(" string, value '%s'\n",sval);
  break;
}

or something like that (not compiled so there could be some silly errors, but this should give you the idea).

IndiraP 13Nov2012 11:01

Re: Handling Variable arguments in C
 
yeah it certainly does help..
Thank U sir..:)


All times are GMT +5.5. The time now is 15:48.