×
Namespaces

Variants
Actions

Archived:Using a variable argument list in Open C

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition (initial release)
Article
Keywords: va_list, va_start(), va_arg(), va_end()
Created: nymanaki (07 Apr 2008)
Last edited: lpvalente (17 Aug 2012)

Contents

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, ...) { }
This page was last modified on 17 August 2012, at 13:31.
45 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×