Question about evaluating the arguments of printf (maybe OT?)

Discussion in 'C++' started by Edith Gross, Nov 2, 2003.

  1. Edith Gross

    Edith Gross Guest

    Let us assume, f1(char *buf) and f1(char *buf) are two functions that
    as a side effect calculate a string. The string will be placed in buf and
    buf is returned by both f1 and f2.

    Should this work:

    char buf[100];
    fprintf(fp,"%s%s",f1(buf),f2(buf));

    Or should I take two different buffers, that is

    char buf1[100],buf2[100];
    fprintf(fp,"%s%s",f1(buf1),f2(buf2));


    TIA,
    EG


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Edith Gross, Nov 2, 2003
    #1
    1. Advertising

  2. Edith Gross

    Moonlit Guest

    Hi,

    "Edith Gross" <> wrote in message
    news:p...
    > Let us assume, f1(char *buf) and f1(char *buf) are two functions that
    > as a side effect calculate a string. The string will be placed in buf and
    > buf is returned by both f1 and f2.
    >
    > Should this work:
    >
    > char buf[100];
    > fprintf(fp,"%s%s",f1(buf),f2(buf));
    >

    What happens is that first one function is called, it stores the string in
    buf and places the return value on the stack, after that the other function
    is called it overwrites the previous calculated string and places the same
    return value on the stack. The printf function will be called with the
    address of buf. The output will be twice the same string.

    > Or should I take two different buffers, that is
    >
    > char buf1[100],buf2[100];
    > fprintf(fp,"%s%s",f1(buf1),f2(buf2));
    >


    This is the correct way.

    >
    > TIA,
    > EG
    >
    >


    Regards, Ron AF Greve.


    > ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

    News==----
    > http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000

    Newsgroups
    > ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

    =---
     
    Moonlit, Nov 2, 2003
    #2
    1. Advertising

  3. Edith Gross escribió:

    > Should this work:
    >
    > char buf[100];
    > fprintf(fp,"%s%s",f1(buf),f2(buf));
    >
    > Or should I take two different buffers, that is
    >
    > char buf1[100],buf2[100];
    > fprintf(fp,"%s%s",f1(buf1),f2(buf2));


    You can avoid the two buffers need bt simply doing:

    fprintf (fp, "%s", f1 (buf) );
    fprintf (fp, "%s", f2 (buf) );

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 2, 2003
    #3
    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. Guest
    Replies:
    5
    Views:
    645
  2. Daniel Ehrenberg
    Replies:
    3
    Views:
    344
    Daniel Ehrenberg
    Jan 7, 2004
  3. Robert Brewer
    Replies:
    3
    Views:
    303
    Samuel Walters
    Jan 8, 2004
  4. Ville Vainio
    Replies:
    11
    Views:
    640
    Hamish Lawson
    Aug 10, 2004
  5. Replies:
    5
    Views:
    302
Loading...

Share This Page