1

How to return a string from the function:

char * temp;

int main()
{
    temp = malloc(129);

    double g_symbol_b_amount = 8536.700000;
    printf("\n value: %s\t ", format_double_trans_amount(double g_symbol_b_amount));
}

char *format_double_trans_amount(double amount)
{
    char amount_array_n1[25]; 
    strcpy(amount_array_n, "");
    sprintf(amount_array_n, "%1f", amount);
    temp = amount_array_n;
    return temp;
}

Here I got the value: 0.000000
I need the orginal value, please help me on this?

2
  • what is amount_array_n? Does this compile for you? Can you show the exact output generated? Commented Apr 30, 2011 at 15:23
  • Brain sorry..i done the formatting. Commented Apr 30, 2011 at 15:39

2 Answers 2

1

I've tested the code below and it behaves correctly. Can you please confirm.

#include "stdio.h"
#include "stdlib.h"

char * temp;

char *format_double_trans_amount(double amount)
{
    sprintf(temp,"%1f",amount);
    return temp;
}

int main()
{
    double g_symbol_b_amount = 8536.700000;
    temp = (char*) malloc( sizeof(char) * 129);
    printf("\n value: %s\n", format_double_trans_amount(g_symbol_b_amount));
    free(temp);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@jcrshankar What environment are you using for compiling/testing the example? I've tested with Linux and gcc, and the value is correctly printed!
@jcrshankar Cool! Do you know what the problem was?
1

Your printf() format string should have %s instead of %f because the value being passed (the value returned from format_double_trans_amount) is a char * and not a double.

I think you should also change your format_double_trans_amount() function to:

char *format_double_trans_amount(double amount)
{      
    sprintf(temp,"%1f",amount);   
    return temp;  
}

5 Comments

Which is all well and good, except amount_array_n in format_double_trans_amount doesn't exist. I suspect he doesn't understand that whole stack vs. heap thing either ;)
@Brian: According to the OP, his code is running and so it must exist, whether we know where or not. However, I've already modified my answer to point out it is not needed.
Neither is returning anything from format_double_trans_amount since temp is global. Ignoring the first problem you pointed out which is of course important, his question is asking how you return a "string" from a function.
@Brian: Your objection appear to be a moving target here :-) but just because a variable is global does not mean it cannot be returned from a function. Clearly, the structure of the code is lacking. But I've corrected the OP's error, and I've made a suggested improvement. I think that's enough to start with.
@jcrshankar: "nowt working" is not enough information to tell what problems you might still be having.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.