C - Variable Argument with Sprintf?

前端 未结 1 1525
南旧
南旧 2021-01-20 16:09

I have a function;

void foo(const char* format, ...)
{
    char buffer[1080];

    // Supposed way to handle C Variable Arguments?
    va_list argptr;
           


        
相关标签:
1条回答
  • 2021-01-20 17:08

    The technique you are apparently attempting to use suggests that you need vsprintf (or, better, vsnprintf)

    va_list argptr;
    va_start(argptr, format);
    vsnprintf(buffer, sizeof buffer, format, argptr);
    va_end(argptr);
    

    That's the very reason such functions from v... group exist in standard library.

    Calling sprintf the way you do it makes no sense at all - it cannot be used with an externally supplied va_list.


    If you want to implement a variable-sized buffer you can do it as follows

    void foo(const char* format, ...)
    {
        static char *buffer;
        static size_t buffer_size;
    
        va_list argptr;
        va_start(argptr, format);
        int length = vsnprintf(buffer, buffer_size, format, argptr);
        va_end(argptr);
    
        if (length + 1 > buffer_size)
        {
          buffer_size = length + 1;
          buffer = realloc(buffer, buffer_size);
          /* Yes, `realloc` should be done differently to properly handle
             possible failures. But that's beside the point in this context */
    
          va_start(argptr, format);
          vsnprintf(buffer, buffer_size, format, argptr);
          va_end(argptr);
        }
    
        printf("%s.\n", buffer);
    }
    

    You can, of course, change the memory management strategy to something different, like use a fixed local buffer of 512 bytes in the first call, and then use a temporary dynamically allocated buffer in the second call only if 512 proves to be insufficient. And so on...

    0 讨论(0)
提交回复
热议问题