accessing the variables in variable length parameters

This is a discussion on accessing the variables in variable length parameters within the C Programming forums, part of the General Programming Boards category; My book does a bad job explaining this topic. The way I understand it, each call to the macro va_arg([variable], ...

  1. #1
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    accessing the variables in variable length parameters

    My book does a bad job explaining this topic. The way I understand it, each call to the macro va_arg([variable], [type]) refers to the next argument in the parameter list marked by va_start. So, does this mean you can only refer to each argument once with va_arg? Is there a way to, say for example, to access the 5th argument, and then reference back to the 2nd argument?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    va_arg(), va_start(), va_end() are used when you need to write a function that takes a variable number of arguments. You need to use the data type va_list so you can store the data items needed by va_start(), va_arg() and va_end().

    example code, from the book that im reading.
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    double Add(int x, ...);
    int main()
    {
    	double d1 =1.5;
    	double d2 =2.5;
    	double d3 =3.5;
    	double d4 = 4.5;
    
    	printf("Given an argument: %2.1f\n",d1);
    	printf("The result returned by Add() is: %2.1f\n\n",
    			Add(1,d1));
    	
    	printf("Given arguments: %2.1f and %2.1f\n", d1,d2);
    	printf("The result returned by Add() is: %2.1f\n\n",
    			Add(2,d1,d2));
    
    	printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1,d2,d3);
    	printf("The result returned by Add() is %2.1f\n\n",
    			Add(3,d1,d2,d3));
    
    	printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n",
    			d1,d2,d3,d4);
    	printf("The result returned by Add() is: %2.1f\n",
    			Add(4,d1,d2,d3,d4));
    	return 0;
    }
    
    /*defintion of Add()*/
    double Add(int x, ...)
    {
    	va_list arglist;
    	int i;
    	double result =0;
    
    	printf("the number of arguments is: %d\n", x);
    	va_start(arglist, x);
    	for( i=0; i<x; i++)
    		result += va_arg(arglist,double);
    	va_end(arglist);
    	return result;
    }
    are you readin the same book that im reading? lol
    Last edited by InvariantLoop; 03-11-2005 at 08:06 PM. Reason: misspelled a word:)
    When no one helps you out. Call google();

  3. #3
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    understood

    for( i=0; i<x; i++)
    result += va_arg(arglist,double);



    what you wrote here, each call to va_arg(arglist,double) refers to the next argument after x. What I'm questioning is how to refer back to these arguments. For example, consider the call
    Add(3,d1,d2,d3) in your program. The first call to va_arg(arglist,double) in Add refers to d1, the second call to va_arg(arglist,double) refers to d2, the next to d3. When this for loop ends, is there a way, say, to call va_arg(arglist,double) again to refer to d1 or d2? That's what I'm asking. I guess you can say I don't understand how va_list arglist behaves.
    Dave

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    You just call the Add() function.
    Code:
    printf("The result returned by Add() is: %2.1f\n\n",
    			Add(1,d1));
    	
    	printf("Given arguments: %2.1f and %2.1f\n", d1,d2);
    	printf("The result returned by Add() is: %2.1f\n\n",
    			Add(2,d1,d2));
    On the first printf Add() takes one argument, then on the second call it takes two, and so on. Maybe I'm not understanding your question.
    When no one helps you out. Call google();

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > I guess you can say I don't understand how va_list arglist behaves.
    It steps over the arguments one at a time in a linear manner.
    If you want to go back to the start for some reason, then you need to reset arglist by calling va_start again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. question regarding accessing variable values
    By do_kev in forum C Programming
    Replies: 2
    Last Post: 03-08-2008, 11:55 AM
  4. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  5. variable length records for strings
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 02-08-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21