1

I want to create this function, but ... parameter lists can't be passed around this way. What is the proper way to go about this?

And yes, I know some compilers provide an 'asprintf'. My question is not what function I should be using, but rather how to get parameter passing like this to work.

// Allocates a formmated string
char *msprintf(const char *format, ...)
{
    int size = snprintf(NULL, 0, format, ...);

    char *buf = (char*)malloc(size);
    snprintf(buf, size, format, ...);

    return buf;
}
user1054922
  • 2,101
  • 2
  • 23
  • 37

1 Answers1

1

You would use vsnprintf() inside the call.

char *msprintf(const char *format, ...) {
    va_list args;
    va_start(args, format);

    int size = vsnprintf(NULL, 0, format, args);
    char *buf = malloc(size);
    vsnprintf(buf, size, format, args);

    va_end(args);
    return buf;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173