Thread: Variable length function parameters

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011

    Variable length function parameters

    Hi all.

    Take a look at the sample code below and note the line:
    ival = va_arg(ap, int)
    Does that mean that ap initially points to the first int in the parameters list?
    Then more importantly, next time va_arg is called, ap will point to the NEXT INT in the parameters list?

    #include <stdarg.h>
    /* minprintf: minimal printf with variable argument list */
    void minprintf(char *fmt, ...)
    	va_list ap; 			/* points to each unnamed arg in turn */
    	char *p, *sval;
    	int ival;
    	double dval;
    	va_start(ap, fmt); 		/* make ap point to 1st unnamed arg */
    	for (p = fmt; *p; p++)
    		if (*p != '%')
    		switch (*++p)
    			case 'd':
    			ival = va_arg(ap, int);
    			printf("%d", ival);
    			case 'f':
    			dval = va_arg(ap, double);
    			printf("%f", dval);
    			case 's':
    			for (sval = va_arg(ap, char *); *sval; sval++)
    va_end(ap); /* clean up when done */
    Disclaimer to CommonTater. I am still learning to use the indent feature so dont blast me yet on code readability!
    Last edited by Vespasian; 09-07-2011 at 03:20 AM.

  2. #2
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    I did research it though, but the "programming nomenclature" and terms Im still finding difficult to grasp

    type va_arg ( va_list ap, type )

    Retrieve next argument
    This macro expands to an expression that has the type type and the value of the next argument in the variable arguments list.

    The next call to this macro will expand to the following argument in the same order as passed to the function.

    Notice that va_arg cannot determine the actual type of the argument passed to the function, but uses whatever type is passed as the type macro argument as its type.

    Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the amount of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read.


    Object of type va_list with information about the additional arguments an their retrieval state. This object shall have been initialized by an initial call to va_start before the first call to va_arg.
    A type name. This type name is used as the type of the expression this macro expands to (i.e., its return type).
    For a type expression to be suitable for its use with va_arg it must be such that when an asterisk (*) would be appended to its right the resulting expression would be a valid pointer type to a type object.

    Return Value
    Returns the next additional argument as an expression of type type.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    From the man page:
    The va_arg() macro expands to an expression that has the type and value
    of the next argument in the call. The parameter ap is the va_list ap
    initialized by va_start(). Each call to va_arg() modifies ap so that the
    next call returns the next argument.
    The parameter type is a type name
    specified so that the type of a pointer to an object that has the speci-
    fied type can be obtained simply by adding a * to type.

    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    No, it assumes that the next argument IS an int.
    And if it isn't an int, then everything gets screwed up after that point.

    va_arg just gets the next param, and uses the type to decide how big that parameter is.
    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.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by Salem View Post
    va_arg just gets the next param, and uses the type to decide how big that parameter is.
    Size of the parameter is not the only concern. Generally, the type specified in the va_arg() usage has to match (or, more precisely, have an identical representation) the type of the actual argument, as there a type conversion tends to happen somewhere in the implementation of va_arg().

    If the supplied argument was (say) a pointer that happens to be the same size as an int, using the result from va_arg(ap, int) gives undefined behaviour (since pointers rarely have the same representation as a signed int).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Last edited by Vespasian; 09-08-2011 at 01:57 AM.

  7. #7
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011

    thanks i think i get it now
    Last edited by Vespasian; 09-08-2011 at 01:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Variable-Length Array to a Function
    By murjax in forum C Programming
    Replies: 4
    Last Post: 07-24-2009, 09:36 PM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. accessing the variables in variable length parameters
    By Dave Jay in forum C Programming
    Replies: 4
    Last Post: 03-13-2005, 01:36 AM
  4. Replies: 8
    Last Post: 07-27-2004, 02:26 AM
  5. function with variable parameters in C
    By jakewendt in forum C Programming
    Replies: 1
    Last Post: 06-25-2002, 08:23 PM