Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Writing functions with variable-length argument list

Posted on 2003-02-20
3
Medium Priority
?
370 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

564 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