Like Tree2Likes
  • 1 Post By quzah
  • 1 Post By grumpy

Variable length function parameters

This is a discussion on Variable length function parameters within the C Programming forums, part of the General Programming Boards category; Hi all. Take a look at the sample code below and note the line: ival = va_arg(ap, int) Does that ...

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

    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?

    Code:
    #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 != '%')
    		{
    			putchar(*p);
    			continue;
    		}
    		switch (*++p)
    		{
    			case 'd':
    			ival = va_arg(ap, int);
    			printf("%d", ival);
    			break;
    			case 'f':
    			dval = va_arg(ap, double);
    			printf("%f", dval);
    			break;
    			case 's':
    			for (sval = va_arg(ap, char *); *sval; sval++)
    			putchar(*sval);
    			break;
    			default:
    			putchar(*p);
    			break;
    		}
    	}
    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
    Posts
    181
    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.

    Parameters

    ap
    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.
    type
    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
    Posts
    14,826
    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.

    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    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).
    Salem likes this.
    Right 98% of the time, and don't care about the other 3%.

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

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

    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, 12: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

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