cascading stdarg calls

Discussion in 'C Programming' started by cornelis van der bent, Nov 27, 2009.

  1. Hi, the code below prints a garbage "world" value to <messages>;
    "Hello " is fine. What can be the problem?

    static char messages[LARGE_ENOUGH];

    int writeText(char* text, char* format, ...)
    {
    va_list argumentList;

    va_start(argumentList, format);
    vsprintf(text, format, argumentList);
    va_end(argumentList);
    }

    void writeMessage(char* format, ...)
    {
    va_list argumentList;

    va_start(argumentList, format);
    writeText(protocolText, format, argumentList);
    va_end(argumentList);
    }

    void test(void)
    {
    writeMessage("Hello %s!\n", "world");
    }

    Thanks,

    Cornelis
     
    cornelis van der bent, Nov 27, 2009
    #1
    1. Advertising

  2. On 27 nov, 15:55, Giacomo Degli Esposti
    <> wrote:
    > The problem is that you cannot call a function with "..." passing
    > it a va_list. You have to create a function accepting a va_list
    > as argument.


    Thanks for the quick answer! It's already in my code now and of
    course works.

    Cornelis
     
    cornelis van der bent, Nov 27, 2009
    #2
    1. Advertising

  3. On 27 nov, 15:59, Ben Bacarisse <> wrote:
    > cornelis van der bent <> writes:
    >
    > > Hi, the code below prints a garbage "world" value to <messages>;
    > > "Hello " is fine.  What can be the problem?

    >
    > This is a FAQ:http://c-faq.com/varargs/handoff.html
    >
    > You have to read the linked FAQs first.


    Thanks for pointing this out. Apologies! I did search google for a
    short period but without succes, but had forgotten to look in the good
    old C-FAQ.

    Cornelis
     
    cornelis van der bent, Nov 27, 2009
    #3
  4. cornelis van der bent

    Seebs Guest

    On 2009-11-27, cornelis van der bent <> wrote:
    > Hi, the code below prints a garbage "world" value to <messages>;
    > "Hello " is fine. What can be the problem?


    A va_list is not the same as a series of arguments. You need to use
    a va_list argument to writeText, not a ...

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 28, 2009
    #4
    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. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    cascading method calls on an object

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    1
    Views:
    2,696
    John C. Bollinger
    Jul 2, 2003
  2. Francesco Bochicchio

    Equivalent of stdarg.h but on the calling side ?

    Francesco Bochicchio, Jul 1, 2003, in forum: C Programming
    Replies:
    5
    Views:
    946
    Mark Gordon
    Jul 4, 2003
  3. Clint Olsen

    Using stdarg with unknown types

    Clint Olsen, Oct 27, 2003, in forum: C Programming
    Replies:
    6
    Views:
    429
    Sheldon Simms
    Oct 29, 2003
  4. Mac A. Cody

    Confusion with stdarg

    Mac A. Cody, Jan 3, 2005, in forum: C Programming
    Replies:
    9
    Views:
    458
    Lawrence Kirby
    Jan 4, 2005
  5. Klaus Schneider

    template with stdarg: enum problem

    Klaus Schneider, Oct 12, 2005, in forum: C++
    Replies:
    8
    Views:
    1,044
    Klaus Schneider
    Oct 12, 2005
Loading...

Share This Page