This page was last modified 06:59, 17 April 2008.
CS000898 - Using a variable argument list
From Forum Nokia Wiki
| ID | CS000898 | Creation date | April 17, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Open C | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): va_list, va_start(), va_arg(), va_end() |
Overview
This code snippet shows some simple examples for using a variable argument list in Open C. The variable argument list can be used in situations where a function that will accept any number of arguments is needed. In the function declaration, optional arguments are indicated by three dots (...), that is, an ellipsis.
In order to read optional arguments, do as follows:
- Declare an object of type va_list.
- Invoke the va_start macro to initialize the list object.
- Invoke the va_arg macro to obtain each of the optional arguments in sequence.
- After reading the argument list, use the va_end macro to clean up the list.
It is also possible to write a function that will take the format string and a variable number of arguments by using functions vprintf, vfprintf or vsprintf in your own function implementation.
Note: In order to use this code, you need to install Open C plug-in.
This snippet can be self-signed.
MMP file
The following libraries are required:
LIBRARY libc.lib
Source file
#include <stdarg.h> //va_list, va_start, va_arg, va_end #include <stdio.h> //printf, fprintf, vfprintf void display_integers(int number, ...); double get_average(int number, ...); void my_printf(char *format, ...); int main(void) { int integer_count = 4; int double_count = 4; double average = 0; char* my_message = "va_list message"; display_integers(integer_count, 1, 2, 3, 4); average = get_average(double_count, 1.2, 2.3, 3.4, 4.5); printf( "get_average returned: %f\n", average); my_printf( "%s\n", my_message); return 0; } void display_integers(int number, ...) { int index=0; va_list arguments; va_start(arguments, number); for (index=0; index<number; index++) { printf("%d\n", va_arg(arguments, int)); } va_end(arguments); } double get_average(int number, ...) { int index=0; double sum=0; double avg=0; va_list arguments; va_start(arguments, number); for (index=0; index<number; index++) { sum+=va_arg(arguments, double); } va_end(arguments); avg = (sum / number); return avg; } void my_printf(char *format, ...) { va_list arguments; fprintf(stdout, "PRINTING: "); va_start(arguments, format); vfprintf(stdout, format, arguments); va_end(arguments); }
Postconditions
Three different example functions that take a variable argument list as parameter are executed and results are displayed to standard output.
See also
Open C documentation points a known problem with a variable list of arguments in macros. For example, the following statement causes a compilation error in some cases:
#define DEBUG(a,...)
The suggested solutions to solve this problem according to the documentation is as follows:
#define DEBUG _DEBUG
static inline void _DEBUG (const char *a, ...) { }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Change the List type of the LIST ??? | divyas | Mobile Java General | 2 | 2006-09-15 20:27 |
| about commandlistener | linbo75 | Mobile Java General | 2 | 2004-02-20 09:00 |
| ringtone list problem | zesky | Symbian Media (Graphics & Sounds) | 6 | 2008-03-20 15:30 |
| error in declaring a variable | Chinu | General Symbian C++ | 12 | 2007-11-26 20:32 |
| getTime, setTime and Long variables | pgoverno | Mobile Java General | 0 | 2004-04-02 18:10 |

