passing strings as pasams and returning them question

M

merrittr

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;
}
 
M

Malcolm McLean

merrittr said:
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.
 
O

osmium

merrittr said:
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.
 

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,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top