va_list

Discussion in 'C Programming' started by ramu, Oct 11, 2007.

  1. ramu

    ramu Guest

    Hi,
    Can we intialize va_list to NULL?
    ie. va_list = NULL

    Regards
     
    ramu, Oct 11, 2007
    #1
    1. Advertising

  2. ramu <> writes:
    > Can we intialize va_list to NULL?
    > ie. va_list = NULL


    Probably not. va_list is "an object type suitable for holding
    information needed by the macros va_start, va_arg, va_end, and
    va_copy"; it's unlikely to be a pointer type.

    What are you trying to accomplish?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 11, 2007
    #2
    1. Advertising

  3. On Thu, 11 Oct 2007 12:37:07 -0700, Keith Thompson wrote:
    > ramu <> writes:
    >> Can we intialize va_list to NULL?
    >> ie. va_list = NULL

    >
    > Probably not. va_list is "an object type suitable for holding
    > information needed by the macros va_start, va_arg, va_end, and va_copy";
    > it's unlikely to be a pointer type.


    I have one implementation where va_list is a typedef for char *, and know
    of one where it's a typedef for void *. Also, implementations are allowed
    to define NULL as 0, which is a valid initialiser for all scalar types. I
    wouldn't say we can "probably" not initialise a va_list to NULL, just
    that it's not portable and a bad idea even on the systems that do allow
    it.
     
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F, Oct 11, 2007
    #3
  4. ramu

    ramu Guest

    On Oct 11, 3:37 pm, Keith Thompson <> wrote:
    > ramu <> writes:
    > > Can we intialize va_list to NULL?
    > > ie. va_list = NULL

    >
    > Probably not. va_list is "an object type suitable for holding
    > information needed by the macros va_start, va_arg, va_end, and
    > va_copy"; it's unlikely to be a pointer type.
    >
    > What are you trying to accomplish?
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Miniss



    Sorry. I meant va_list ap=NULL;
    Not va_list = NULL;
     
    ramu, Oct 11, 2007
    #4
  5. ramu <> writes:
    > On Oct 11, 3:37 pm, Keith Thompson <> wrote:
    >> ramu <> writes:
    >> > Can we intialize va_list to NULL?
    >> > ie. va_list = NULL

    >>
    >> Probably not. va_list is "an object type suitable for holding
    >> information needed by the macros va_start, va_arg, va_end, and
    >> va_copy"; it's unlikely to be a pointer type.
    >>
    >> What are you trying to accomplish?

    >
    > Sorry. I meant va_list ap=NULL;
    > Not va_list = NULL;


    Please don't quote signatures (the stuff following the "-- " line)
    unless you're actually commenting on them.

    I assumed that was what you meant; I actually missed the fact that you
    assigned a value to a type rather than to a variable.

    But again, va_list is not necessarily a pointer type, and assigning a
    null value to a va_list object, as far as I can tell, doesn't make any
    sense.

    And again, what are you trying to accomplish?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 11, 2007
    #5
  6. ramu

    ramu Guest

    On Oct 12, 2:39 am, Keith Thompson <> wrote:
    > ramu <> writes:
    > > On Oct 11, 3:37 pm, Keith Thompson <> wrote:
    > >> ramu <> writes:
    > >> > Can we intialize va_list to NULL?
    > >> > ie. va_list = NULL

    >
    > >> Probably not. va_list is "an object type suitable for holding
    > >> information needed by the macros va_start, va_arg, va_end, and
    > >> va_copy"; it's unlikely to be a pointer type.

    >
    > >> What are you trying to accomplish?

    >
    > > Sorry. I meant va_list ap=NULL;
    > > Not va_list = NULL;

    >
    > Please don't quote signatures (the stuff following the "-- " line)
    > unless you're actually commenting on them.
    >
    > I assumed that was what you meant; I actually missed the fact that you
    > assigned a value to a type rather than to a variable.
    >
    > But again, va_list is not necessarily a pointer type, and assigning a
    > null value to a va_list object, as far as I can tell, doesn't make any
    > sense.
    >
    > And again, what are you trying to accomplish?
    >


    Am getting Lint warning saying that variable is not initialized.
    So am trying to initialize ap to NULL.
     
    ramu, Oct 12, 2007
    #6
  7. ramu <> wrote:
    > Keith Thompson <> wrote:
    > > > > > Can we intialize va_list to NULL?

    > >
    > > ... va_list is not necessarily a pointer type, and
    > > assigning a null value to a va_list object, as far as I
    > > can tell, doesn't make any sense.
    > >
    > > And again, what are you trying to accomplish?

    >
    > Am getting Lint warning saying that variable is not
    > initialized. So am trying to initialize ap to NULL.


    va_list ap = { 0 };

    You can initialise _any_ object with { 0 }. However, if you're
    just doing this to silence one warning, realise that applying
    a fix may simply raise an alternative warning.

    Apart from fixing actual errors, you shouldn't be letting
    implementations (or tools thereof) dictate how you write
    your code.

    Too many programmers fall into the trap of adding needless
    casts because a compiler is beeping at them without it.
    Because they haven't checked whether there is an actual
    problem with the code, they inadvertently add one thinking
    that if the compiler has stopped complaining, the code
    must be correct.

    --
    Peter
     
    Peter Nilsson, Oct 12, 2007
    #7
  8. ramu <> writes:
    > On Oct 12, 2:39 am, Keith Thompson <> wrote:
    >> ramu <> writes:
    >> > On Oct 11, 3:37 pm, Keith Thompson <> wrote:
    >> >> ramu <> writes:
    >> >> > Can we intialize va_list to NULL?
    >> >> > ie. va_list = NULL

    >>
    >> >> Probably not. va_list is "an object type suitable for holding
    >> >> information needed by the macros va_start, va_arg, va_end, and
    >> >> va_copy"; it's unlikely to be a pointer type.

    >>
    >> >> What are you trying to accomplish?

    >>
    >> > Sorry. I meant va_list ap=NULL;
    >> > Not va_list = NULL;

    >>
    >> Please don't quote signatures (the stuff following the "-- " line)
    >> unless you're actually commenting on them.
    >>
    >> I assumed that was what you meant; I actually missed the fact that you
    >> assigned a value to a type rather than to a variable.
    >>
    >> But again, va_list is not necessarily a pointer type, and assigning a
    >> null value to a va_list object, as far as I can tell, doesn't make any
    >> sense.
    >>
    >> And again, what are you trying to accomplish?

    >
    > Am getting Lint warning saying that variable is not initialized.
    > So am trying to initialize ap to NULL.


    I'm guessing that you're getting a warning when you try to use it, not
    on the declaration itself.

    Initializing ap to NULL is not the solution to your problem. The way
    to initialize ap (an object of type va_list) is to use the va_start
    macro. Better yet, you should read the documetation (your textbook or
    whatever) rather than just guessing.

    Here's an example from the C99 standard:

    #include <stdarg.h>
    #define MAXARGS 31

    void f1(int n_ptrs, ...)
    {
    va_list ap;
    char *array[MAXARGS];
    int ptr_no = 0;
    if (n_ptrs > MAXARGS)
    n_ptrs = MAXARGS;
    va_start(ap, n_ptrs);
    while (ptr_no < n_ptrs)
    array[ptr_no++] = va_arg(ap, char *);
    va_end(ap);
    f2(n_ptrs, array);
    }

    If that doesn't help, please post your actual code and the exact text
    of any warnings you get.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 12, 2007
    #8
  9. Keith Thompson wrote:
    >
    > ramu <> writes:

    [...]
    > > Am getting Lint warning saying that variable is not initialized.
    > > So am trying to initialize ap to NULL.

    >
    > I'm guessing that you're getting a warning when you try to use it, not
    > on the declaration itself.
    >
    > Initializing ap to NULL is not the solution to your problem. The way
    > to initialize ap (an object of type va_list) is to use the va_start
    > macro. Better yet, you should read the documetation (your textbook or
    > whatever) rather than just guessing.
    >
    > Here's an example from the C99 standard:
    >
    > #include <stdarg.h>
    > #define MAXARGS 31
    >
    > void f1(int n_ptrs, ...)
    > {
    > va_list ap;

    [...]
    > va_start(ap, n_ptrs);

    [...]
    > If that doesn't help, please post your actual code and the exact text
    > of any warnings you get.


    Perhaps his lint isn't smart enough to know that va_start() will
    initialize ap, rather than pass its value to a function? (Or,
    perhaps, he's simply not using it correctly. As you say, actual
    code will help determine the "real" problem here.)

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Oct 12, 2007
    #9
    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. va_list in JNI

    , Jun 23, 2005, in forum: Java
    Replies:
    4
    Views:
    2,361
  2. Rich Herrick

    reference to va_list

    Rich Herrick, Jan 16, 2005, in forum: C++
    Replies:
    0
    Views:
    440
    Rich Herrick
    Jan 16, 2005
  3. Peter

    va_list help, please ...

    Peter, Feb 15, 2005, in forum: C++
    Replies:
    6
    Views:
    3,559
    Pete Becker
    Feb 15, 2005
  4. John Guo

    va_list help

    John Guo, Mar 31, 2005, in forum: C++
    Replies:
    5
    Views:
    3,323
    Pete Becker
    Mar 31, 2005
  5. Douwe
    Replies:
    3
    Views:
    753
    Chris Torek
    Aug 30, 2003
Loading...

Share This Page