?
Solved

Writing functions with variable-length argument list

Posted on 2003-02-20
3
Medium Priority
?
364 Views
Last Modified: 2010-04-01
Im looking for a way to pass a variable-length argument list from one function to another.

Let me give an example. Consider the following function header:

void func(char *format, ...);

where "format" is a printf-like format string and the "..." is the argument list that corresponds to the "format" string (as in printf). Now, from this I want to be able to pass these arguments down to the printf/sprintf/fprintf functions. The function "func" would therefore look something like this:

void func(char *format, ...)
{
   // second argument to printf should be the
   // variable-length argument list sent to func
   printf(format, [...]);
}

I think Ive understood how variable-length argument lists works, but I have not managed to get this to work. My question is therefore: Is this possible? If it is possible, how do I do it?

Thanks,
dedsi
0
Comment
Question by:dedsi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
thienpnguyen earned 400 total points
ID: 7990005
Inside "void func(char *format, ...)", you need to use vprintf (or vsprintf ....) not printf


The following example gets from C++ Builder Help file, but it is also ok with other compiler .


#include <stdio.h>
#include <stdarg.h>

int vpf(char *fmt, ...)
{
   va_list argptr;
   int cnt;

   va_start(argptr, fmt);
   cnt = vprintf(fmt, argptr);
   va_end(argptr);

   return(cnt);
}

int main(void)
{
   int inumber = 30;
   float fnumber = 90.0;
   char *string = "abc";

   vpf("%d %f %s\n",inumber,fnumber,string);

   return 0;
}


0
 
LVL 2

Expert Comment

by:keenez
ID: 7990043
Dedsi,

You should use stdarg.h library.

In it are some macros, etc .... to help.

Eg.

#include <stdarg.h>
double average(int i, ...) {

    double total = 0;
    int j:
    va_list li;

    va_start(li, i);

    for (j = 1; j <= i; j++) {
        total = total + va_arg(li, double);
    }

    va_end(li);

    return total / i;
}

Cheers,

Keenez
0
 
LVL 1

Author Comment

by:dedsi
ID: 7990208
thienpnguyen,

thank you for the quick reply, your solution works perfectly.

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question