Functions accepting a variable number of arguments.
Tutorial Details:
Variable argument lists
Perhaps you would like to have a function that will accept any number of values and then return the average. You don't know how many arguments will be passed in to the function. One way you could make the function would be to accept a pointer to an array. Another way would be to write a function that can take any number of arguments. So you could write avg(4, 12.2, 23.3, 33.3, 12.1); or you could write avg(2, 2.3, 34.4); Some library functions can accept a variable list of arguments (such as the venerable printf).
To use a function with variable number of arguments, or more precisely, a function without a set number of arguments, you would use the cstdarg header file. There are four parts needed: va_list, which stores the list of arguments, va_start, which initializes the list, va_arg, which returns the next argument in the list, and va_end, which cleans up the variable argument list.
Whenever a function is declared to have an indeterminate number of arguments, in place of the last argument you should place an ellipsis (which looks like '...'), so, int a_function ( int x, ... ); would tell the compiler the function should accept however many arguments that the programmer uses, as long as it is equal to at least one, the one being the first, x.
va_list is like any other variable. For example,
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: C and Cpp Programming in C and Cpp Variable argument lists Tutorial
View Tutorial: C and Cpp Programming in C and Cpp Variable argument lists Tutorial
Related
Tutorials:
Java Tip 71: Use
dynamic messaging in Java - JavaWorld - April
1999
Java Tip 71: Use
dynamic messaging in Java - JavaWorld - April
1999 |
Smart object-management saves the
day - JavaWorld November 1999
Smart object-management saves the
day - JavaWorld November 1999 |
JavaWorld article about
JavaCC
JavaWorld article about
JavaCC |
Behold the power
of parametric
polymorphism - JavaWorld February
2000
Behold the power
of parametric
polymorphism - JavaWorld February
2000 |
Eliminate tedious programming: Recover data with XML and Reflection - JavaWorld November
2000
Eliminate tedious programming: Recover data with XML and Reflection - JavaWorld November
2000 |
C# : A language alternative or just J--? (part1)
C# : A language alternative or just J--? (part1) |
C#: A language alternative or just J--?, Part 2 - JavaWorld December 2000
C#: A language alternative or just J--?, Part 2 - JavaWorld December 2000 |
iContract: Design by Contract in Java - JavaWorld February
2001
iContract: Design by Contract in Java - JavaWorld February
2001 |
Device programming with MIDP, Part
2 - JavaWorld
March 2001
Device programming with MIDP, Part
2 - JavaWorld
March 2001 |
Embed Java code into your
native apps -
JavaWorld May 2001
Embed Java code into your
native apps -
JavaWorld May 2001 |
Language improvements
and models make
great Java - JavaWorld
Language improvements
and models make
great Java - JavaWorld |
Integrate Java and C++ with Jace
Integrate Java and C++ with Jace |
Java's character and assorted string
classes support text-processing
Java's character and assorted string
classes support text-processing |
replacing implementation inheritance by contract
inheritance
replacing implementation inheritance by contract
inheritance |
Creating Varargs in Java 1.5 Tiger
Creating Varargs in Java 1.5 Tiger
In this excerpt from Chapter 5 of the book, Brett and David cover how to create and iterate over variable-length argument lists (better known as varargs), which will have you writing better, cleaner, more flexible code |
JTwain
JTwain will implement a Java interface to the the Win32 C DLL TWAIN acquire methods. |
Servlets and JavaServer Pages (JSP) : A Tutorial
An excellent tutorial on JSP and Servlets. |
Java Technology Fundamentals
Learn how to use variable arguments lists, the new JConsole utility to monitor applications, and discover ways to learn what you need to know to get Sun certified. |
10 Minutes Guide to Ant
10 Minutes Guide to Ant
10 Minutes Guide to Ant
Previous Tutorial Index Next
Introduction
Well for the next 10 minutes get ready to devote to the ant guide. This will make some sence to the ant.
Ant is a free tool under GNU Licence and is |
New Technical Articles: 64-bit Programming on Solaris 10 OS for x86 Platforms
Four technical articles describe the new Sun Studio 10 software's 64-bit programming features on the Solaris 10 OS for x86 and AMD64 platforms. Important issues regarding the AMD64 ABI (Application Binary Interface), debugging, migration to 64-bits, and p |
|
|
|