Calling a function that expects variable arguments from a functionwith variable arguments

Discussion in 'C Programming' started by Navaneeth, Nov 19, 2010.

  1. Navaneeth

    Navaneeth Guest

    I am writing a dynamically growing string buffer. The following struct
    represents a string buffer.

    struct strbuf {
    char *buffer; /* null terminated buffer */
    size_t length; /* length of the string excluding null
    terminator */
    size_t allocated; /* total memory allocated */
    };

    I have also got several functions to operate on this. The following
    function takes a format and formats it using "vsnprintf" and adds to
    the buffer.

    int strbuf_addf(struct strbuf *string, const char *format, ...)
    {
    size_t space_available;
    int length;
    va_list args;

    if(string == NULL) return 0;

    space_available = get_available_space(string);
    for(;;)
    {
    va_start(args, format);
    length = vsnprintf(string->buffer + string->length,
    space_available, format, args);
    va_end(args);

    if(length < space_available) break;

    grow_buffer(string);
    space_available = get_available_space(string);
    }

    string->length += length;

    /* buffer should always contain some free space */
    assert(string->length < string->allocated);

    return 1;
    }

    This works well. Now I have a requirement where I would like to call
    the above function from another function that takes variable
    arguments. Prototype would be,

    void print_info(const char *format, ...);

    How can I call "strbuf_addf" from "print_info"?

    The only way which I can think is add another method to the string
    buffer which takes a "va_list". So from "print_info" & "strbuf_addf" I
    can get a "va_list" and pass that. Wondering is this the correct
    approach?

    Any help would be great!
    Navaneeth, Nov 19, 2010
    #1
    1. Advertising

  2. Navaneeth

    Paul N Guest

    Re: Calling a function that expects variable arguments from afunction with variable arguments

    On Nov 19, 5:19 am, Navaneeth <> wrote:
    > I am writing a dynamically growing string buffer. The following struct
    > represents a string buffer.
    >
    > struct strbuf {
    >     char *buffer;            /* null terminated buffer */
    >     size_t length;           /* length of the string excluding null
    > terminator */
    >     size_t allocated;        /* total memory allocated */
    >
    > };
    >
    > I have also got several functions to operate on this. The following
    > function takes a format and formats it using "vsnprintf" and adds to
    > the buffer.
    >
    > int strbuf_addf(struct strbuf *string, const char *format, ...)
    > {
    >     size_t space_available;
    >     int length;
    >     va_list args;
    >
    >     if(string == NULL) return 0;
    >
    >     space_available = get_available_space(string);
    >     for(;;)
    >     {
    >         va_start(args, format);
    >         length = vsnprintf(string->buffer + string->length,
    > space_available, format, args);
    >         va_end(args);
    >
    >         if(length < space_available) break;
    >
    >         grow_buffer(string);
    >         space_available = get_available_space(string);
    >     }
    >
    >     string->length += length;
    >
    >     /* buffer should always contain some free space */
    >     assert(string->length < string->allocated);
    >
    >     return 1;
    >
    > }
    >
    > This works well. Now I have a requirement where I would like to call
    > the above function from another function that takes variable
    > arguments. Prototype would be,
    >
    > void print_info(const char *format, ...);
    >
    > How can I call "strbuf_addf" from "print_info"?
    >
    > The only way which I can think is add another method to the string
    > buffer which takes a "va_list". So from "print_info" & "strbuf_addf" I
    > can get a "va_list" and pass that. Wondering is this the correct
    > approach?
    >
    > Any help would be great!


    I'm not an expert but...

    You can't *call* a function with a variable number of arguments. That
    is to say, whenever you make a function call, there will be a fixed
    number of arguments present. So when print_info calls strbuf_addf it
    must send a fixed number of arguments, even if strbuf_addf can also
    take a different number of arguments. So I think you are going to have
    to pass the variable arguments from print_info to strbuf_addf in the
    form of a va_list. From your example code it seems you already know
    how to do this, or at least better than I do.

    Hope that helps.
    Paul.
    Paul N, Nov 19, 2010
    #2
    1. Advertising

  3. Re: Calling a function that expects variable arguments from a function with variable arguments

    In article <-september.org>,
    High Road to China Blue <> wrote:
    ....
    >In nonstandard C, gcc has an interface to construct a function call.


    Please elaborate. To what do you refer?

    --
    "We should always be disposed to believe that which appears to us to be
    white is really black, if the hierarchy of the church so decides."

    - Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -
    Kenny McCormack, Nov 19, 2010
    #3
  4. Navaneeth

    Nobody Guest

    Re: Calling a function that expects variable arguments from a function with variable arguments

    On Fri, 19 Nov 2010 23:38:45 +0000, Kenny McCormack wrote:

    > In article <-september.org>,
    > High Road to China Blue <> wrote:
    > ...
    >>In nonstandard C, gcc has an interface to construct a function call.

    >
    > Please elaborate. To what do you refer?


    http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/Constructing-Calls.html

    This allows you to call a function with (a copy of) the argument list of
    the current function.

    It doesn't allow you to construct arbitrary calls. If you need that,
    there's always libffi.
    Nobody, Nov 20, 2010
    #4
  5. Re: Calling a function that expects variable arguments from a function with variable arguments

    In article <>,
    Nobody <> wrote:
    >On Fri, 19 Nov 2010 23:38:45 +0000, Kenny McCormack wrote:
    >
    >> In article <-september.org>,
    >> High Road to China Blue <> wrote:
    >> ...
    >>>In nonstandard C, gcc has an interface to construct a function call.

    >>
    >> Please elaborate. To what do you refer?

    >
    >http://gcc.gnu.org/onlinedocs/gcc-4.5.1/gcc/Constructing-Calls.html
    >
    >This allows you to call a function with (a copy of) the argument list of
    >the current function.


    Interesting...

    >It doesn't allow you to construct arbitrary calls. If you need that,
    >there's always libffi.


    Yes. I'm a big fan of "avcall" (part of libffi)

    --
    Is God willing to prevent evil, but not able? Then he is not omnipotent.
    Is he able, but not willing? Then he is malevolent.
    Is he both able and willing? Then whence cometh evil?
    Is he neither able nor willing? Then why call him God?
    ~ Epicurus
    Kenny McCormack, Nov 20, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nass
    Replies:
    2
    Views:
    324
    Larry Smith
    Feb 15, 2007
  2. Cirene
    Replies:
    7
    Views:
    332
    Paul Shapiro
    Jun 7, 2008
  3. Timothy Crone

    Unpack Expects the Wrong Number of Bytes

    Timothy Crone, Jul 18, 2009, in forum: Python
    Replies:
    2
    Views:
    378
    Ross Ridge
    Jul 18, 2009
  4. Seebs
    Replies:
    9
    Views:
    3,970
    Phil Carmody
    Sep 17, 2009
  5. Jack Bates
    Replies:
    0
    Views:
    219
    Jack Bates
    Sep 9, 2011
Loading...

Share This Page