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

E

Edith Gross

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
 
M

Moonlit

Hi,

Edith Gross said:
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.

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
=---
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top