473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

passing strings as pasams and returning them question

Hi all,

i am struggling with c strings still here is what i want, pass in 2
string vals convert them to doubles add them
then convert the result back to a char string and return it to a
printf statement.
can you see where I am going wrong?

printf("%s\n",add("d","3","4"));
char *add(char ctype, char *i, char *j)
{
double x,y,z;
char retval[10];
printf("%s %s %s\n",retval,i,j);
z = atof(i)+atof(j);
sprintf( retval, "%i",z );
printf("%s %d %d\n",retval,atof(i),atof(j));
return retval;
}

May 20 '07 #1
2 1654

"merrittr" <me******@gmail.comwrote in message
news:11********************@b40g2000prd.googlegrou ps.com...
Hi all,

i am struggling with c strings still here is what i want, pass in 2
string vals convert them to doubles add them
then convert the result back to a char string and return it to a
printf statement.
can you see where I am going wrong?

printf("%s\n",add("d","3","4"));
char *add(char ctype, char *i, char *j)
{
double x,y,z;
char retval[10];
printf("%s %s %s\n",retval,i,j);
z = atof(i)+atof(j);
sprintf( retval, "%i",z );
printf("%s %d %d\n",retval,atof(i),atof(j));
return retval;
}
You've basically got the idea.

What is the ctype variable supposed to represent? If you treat all numbers
as reals you don't need it. Even if the numbers are in fact integers, almost
certainly it will no harm to treat them as doubles.

Use %g or %f to convert a double to a sprintf() field.
Finally, you return the address of a local variable. The best way round this
is to pass in the buffer for the return string. That would mean breaking up
the printf() statement, which is probably a good thing. Alternatively you
could allocate with malloc() and return it, but then you need to free, or
return a static buffer, which will work in this context but will be
rewritten on every call, so isn't a good idea for modular code.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
May 20 '07 #2
"merrittr" writes:
i am struggling with c strings still here is what i want, pass in 2
string vals convert them to doubles add them
then convert the result back to a char string and return it to a
printf statement.
can you see where I am going wrong?

printf("%s\n",add("d","3","4"));
char *add(char ctype, char *i, char *j)
{
double x,y,z;
char retval[10];
printf("%s %s %s\n",retval,i,j);
z = atof(i)+atof(j);
sprintf( retval, "%i",z );
printf("%s %d %d\n",retval,atof(i),atof(j));
return retval;
}
The first thing I notice is that you are returning a pointer to an array
that may no longer exist by the time it gets back to the caller. Change to
"static char retval[10];" retval is you have it is an auto variable, q.v.

Better yet, allocate an array in the caller and let the callee change the
values in that array. I suppose there is more, but first things first. I
think I see some abandoned variables.
May 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
2895
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
39
7591
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
10
4581
by: ptq2238 | last post by:
Hi, Tried this code to assist my understanding of strings and functions but I'm not sure why the errors are occurring and hope someone can shed some light to my learning. #include <stdio.h>...
3
364
by: merrittr | last post by:
Hi all, i am struggling with c strings still here is what i want, pass in 2 string vals convert them to doubles add them then convert the result back to a char string and return it to a printf...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.