Handling Variable arguments in C

Discussion in 'C' started by IndiraP, Nov 10, 2012.

  1. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    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: ???
     
    Last edited by a moderator: Nov 11, 2012
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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");
    
     
  3. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    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???
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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 likes this.
  5. IndiraP

    IndiraP New Member

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    2
    Trophy Points:
    0
    yeah it certainly does help..
    Thank U sir..:)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice