passing strings as pasams and returning them question

Discussion in 'C Programming' started by merrittr, May 20, 2007.

  1. merrittr

    merrittr Guest

    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;
    }
     
    merrittr, May 20, 2007
    #1
    1. Advertising

  2. On 20 May 2007 14:11:18 -0700, merrittr <> wrote:

    >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"));


    You said two values above but here you pass three. You need to make
    up your mind.

    >
    >
    >char *add(char ctype, char *i, char *j)


    You say the first parameter is a char. But the corresponding argument
    in the calling statement above is a string. You need to make up your
    mind. You might also ask yourself why you are defining a parameter
    you don't use in the function.

    >{
    > double x,y,z;
    > char retval[10];
    > printf("%s %s %s\n",retval,i,j);
    > z = atof(i)+atof(j);
    > sprintf( retval, "%i",z );


    z is a double. %i is used only to print an int.

    > printf("%s %d %d\n",retval,atof(i),atof(j));


    atof returns a double. %d is used only to print an int.

    > return retval;


    Others have pointed out the problem here.

    >}



    Remove del for email
     
    Barry Schwarz, May 21, 2007
    #2
    1. Advertising

  3. merrittr

    merrittr Guest

    Thanks all,
    I am still trying to digest all the advice
    I havr the retval definded as static (but I think i will
    predefine it and have the function operate on it).

    One new problem I am having is shown below. I can get the vars into
    the
    add function but atof on them sets them to 0 am I using atof
    correctly?

    oh one more thing ctype will be a single char i,d,s (int double
    string).



    char *add(char *ctype, char *i, char *j)
    {
    double x,y,z,a;
    static char retval[10]="retval";
    printf("---%s %s %s\n",retval,i,j);-->i get ---retval 3.0 4.0
    x=atof(i);
    y=atof(j);
    z=x+y;
    sprintf( retval, "%f",z );
    printf("z%f\n",z ); -->i get z0♀
    a = atof( "42.0is_the_answer" );
    printf("ret:%s x:%g y:%g z:%g a:%g\n",retval,x,y,z,a);-->i get ret:
    0.000000 x:0 y:0 z:0 a:0
    return retval;
    }
     
    merrittr, May 21, 2007
    #3
  4. On 20 May 2007 20:54:08 -0700, merrittr <> wrote:

    >Thanks all,
    >I am still trying to digest all the advice
    >I havr the retval definded as static (but I think i will
    >predefine it and have the function operate on it).
    >
    >One new problem I am having is shown below. I can get the vars into
    >the
    >add function but atof on them sets them to 0 am I using atof
    >correctly?


    We can't tell unless you provide a complete compilable example that
    demonstrates the problem.

    >
    >oh one more thing ctype will be a single char i,d,s (int double
    >string).


    If it is a single char and you don't intend to change it, why not
    declare it as char instead of char*? In your previous code, the
    calling statement attempted to pass "d" which is not a single char. If
    you want a single char, use 'd'.

    >
    >
    >
    >char *add(char *ctype, char *i, char *j)
    >{
    > double x,y,z,a;
    > static char retval[10]="retval";
    > printf("---%s %s %s\n",retval,i,j);-->i get ---retval 3.0 4.0


    Did you remember to include stdio.h?

    > x=atof(i);


    Did you remember to include stdlib.h?

    > y=atof(j);
    > z=x+y;
    > sprintf( retval, "%f",z );
    > printf("z%f\n",z ); -->i get z0?
    > a = atof( "42.0is_the_answer" );
    > printf("ret:%s x:%g y:%g z:%g a:%g\n",retval,x,y,z,a);-->i get ret:
    >0.000000 x:0 y:0 z:0 a:0
    > return retval;
    >}



    Remove del for email
     
    Barry Schwarz, May 22, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    25
    Views:
    892
    E. Robert Tisdale
    Mar 23, 2005
  2. Anonieko

    HttpHandlers - Learn Them. Use Them.

    Anonieko, Jun 15, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    520
    tdavisjr
    Jun 16, 2006
  3. merrittr
    Replies:
    2
    Views:
    269
    osmium
    May 20, 2007
  4. Ramon F Herrera
    Replies:
    8
    Views:
    481
    Pascal J. Bourguignon
    Sep 13, 2009
  5. why the lucky stiff
    Replies:
    5
    Views:
    146
    why the lucky stiff
    Sep 22, 2004
Loading...

Share This Page