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;
} 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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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>...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |