473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1649

"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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.