Jump to content
  • Advertisement
Sign in to follow this  
Krynnish Conspiracy

Functions with unlimited amout of parameters

This topic is 4903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

(This refers to C) Obviously it is possible to create one - prtinf() is such. I need to write such a function, but alas, I can't seem to get it. I know it has something to do with [...] in the parameter line. I tried using a pointer to the end of the last "must-have" parameter and reading that, but it always read zero. So, what do I do? Thanks ahead, and may your browser never show a 404.

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately its a complex theme. Try looking for the macros:
va_start
va_arg
va_end

Luck!
Guimo

Share this post


Link to post
Share on other sites
This example might help. It's a simple attempt to emulate printf. Kinda pointless though since it uses printf.

#include <stdarg.h>

void ownPrintf(const char *format, ...) {
char c;
va_list args;

va_start(format, args);

while(c = *format++) {
if(c != '%') {
putchar(c);
}

switch(*format++) {
case 'd': {
int value = va_arg(args, int);
printf("%d", value);
} break;

case 'f': {
double value = va_arg(args, double);
printf("%f", value);
} break;

case 's': {
const char *value = va_arg(args, const char *);
printf("%s", value);
} break;

case '%': {
printf("%%");
} break;
}
}

va_end(args);
}
Beware of that type promotion will extend small integer types to ints and floating point constants to double.
The behaviour for non-POD types is undefined. So it's useless for sending complex objects.

Share this post


Link to post
Share on other sites
There are a set of macros to extract arguments from a variable argument list.

#include <stdarg.h>


The relevant ones are:

va_list
va_start
va_end
va_arg

You can make your own printf function by calling vsprintf() after initializing the variable argument list:


void myprintf (const char* fmt, ...)
{
va_list ap;
char buffer[1024]; // better make this big enough... hello buffer overflow

va_start(ap, fmt)
vsprintf(buffer, fmt, ap);
va_end(ap);

// do something with buffer

}


Share this post


Link to post
Share on other sites
See other user comments if you're indeed just using C. If you're using C++ there's many much better options available to you. Example of what I replace printf with:

#include <boost/format.hpp>
#include <iostream>
using namespace boost;
using namespace std;

cout << format("I like %1% oh yes I do.") % "pie" << endl;


Note that boost::format is a typesafe and object-safe replacement for printf. It also works with strings:

#include <string>
string my_string = str( format("%1% to the %2%, yo.") % "Shnizle" % "nizzle" );


The idea is that you create an intermediate type. The boost library has another tool of use for the general case called Boost.Assignment. Example straight from their docs, with a minor fix:

#include <vector>
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/assert.hpp>
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope

{
vector<int> values;
values += 1,2,3,4,5,6,7,8,9; // insert values at the end of the container
BOOST_ASSERT( values.size() == 9 );
BOOST_ASSERT( values[0] == 1 );
BOOST_ASSERT( values[8] == 9 );
}


One could do something like this:

void function( const std::vector< int > & some_arguments );
function((vector< int >() += 1,2,3,4,5,6,7,8,9));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!