Re: va_arg() question

Discussion in 'C Programming' started by Eric Sosman, Aug 7, 2003.

  1. Eric Sosman

    Eric Sosman Guest

    wrote:
    >
    > Hello all,
    >
    > I have a quick question regarding the use of va_arg() with multiple
    > parameter definitions. If the parameters are of different types, how
    > do I know what type it is when using va_arg() ????
    >
    > For example, if I was to write a function similar to the good old
    > printf() which takes multiple parameters of different types (eg. int,
    > char*, etc) how do I know whether the parameter I'm processing is an
    > int or a char* ???


    You must figure out the type of the argument before
    using va_arg() to retrieve its value. printf() can do this
    by inspecting the format string; for example,

    printf ("%s = %d (%.2f%%)\n",

    implies that the second through fourth arguments must be a
    `char*', an `int', and a `double', respectively.

    Note that the arguments corresponding to `...' are subject
    to promotion. That is, if you pass a `float' value it will be
    promoted to `double' and you must retrieve it as such. There
    are a few unfortunate situations where the promotion rules are
    implementation-defined -- for example, `unsigned short' might
    promote to `int' or to `unsigned int' -- and in such cases you
    can't write 100% portable code.

    --
     
    Eric Sosman, Aug 7, 2003
    #1
    1. Advertising

  2. Eric Sosman

    Kevin Easton Guest

    Eric Sosman <> wrote:
    > wrote:
    >>
    >> Hello all,
    >>
    >> I have a quick question regarding the use of va_arg() with multiple
    >> parameter definitions. If the parameters are of different types, how
    >> do I know what type it is when using va_arg() ????
    >>

    [...]
    > Note that the arguments corresponding to `...' are subject
    > to promotion. That is, if you pass a `float' value it will be
    > promoted to `double' and you must retrieve it as such. There
    > are a few unfortunate situations where the promotion rules are
    > implementation-defined -- for example, `unsigned short' might
    > promote to `int' or to `unsigned int' -- and in such cases you
    > can't write 100% portable code.


    Sure you can - just specify that your variadic function takes an int or
    unsigned int, and not one of the ambiguously-promoted types. Then it's
    up to the caller to ensure that the correct type is passed, probably
    with a cast.

    - Kevin.
     
    Kevin Easton, Aug 8, 2003
    #2
    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. Gordon Burditt

    Re: va_arg() question

    Gordon Burditt, Aug 7, 2003, in forum: C Programming
    Replies:
    0
    Views:
    414
    Gordon Burditt
    Aug 7, 2003
  2. Artie Gold

    Re: va_arg() question

    Artie Gold, Aug 7, 2003, in forum: C Programming
    Replies:
    0
    Views:
    433
    Artie Gold
    Aug 7, 2003
  3. Mike Wahler

    Re: va_arg() question

    Mike Wahler, Aug 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    411
    Dave Thompson
    Aug 11, 2003
  4. mdh

    va_arg question

    mdh, Sep 11, 2008, in forum: C Programming
    Replies:
    5
    Views:
    388
    Nick Keighley
    Sep 12, 2008
  5. va_arg() question

    , Jul 31, 2013, in forum: C Programming
    Replies:
    2
    Views:
    226
    James Kuyper
    Jul 31, 2013
Loading...

Share This Page