-2
R=10;
LPCSTR cs;
string s;
stringstream ss;
ss<<R;
s = ss.str();
cout << cs <<endl;

Will give me the console output 10, like it should be.

Now I wanted to put this into a function:

const char * doubleToLPSTR(double x){
string s;
stringstream ss;
ss << x;
s = ss.str();
return s.c_str();

}

But

R = 10;
LPCSTR cs;
string s;
cs = doubleToLPSTR(R);
cout << cs << endl;

Returns does not work.... Why???

Thank you for your help, like this?

const char * doubleToLPSTR(double x){

const int size = 20;
char *cs = new char[size];

string s;
stringstream ss;
ss << x;
s = ss.str();
const char * tempAr = s.c_str();


for (int i = 0; i < size; i++){

    cs[i] = tempAr[i];

}

return cs;
}
newandlost
  • 935
  • 2
  • 10
  • 21
  • What type is R? Do you want to use it as a parameter to a function? what exactly are you trying to accomplish, and what is the problem? – Tommy Andersen Aug 08 '14 at 11:31
  • possible duplicate of [Double to Const Char\*](http://stackoverflow.com/questions/6404586/double-to-const-char) – Leo Chapiro Aug 08 '14 at 11:31
  • Maybe the function you are looking for is [`std::to_string`](http://www.cplusplus.com/reference/string/to_string/). – nwp Aug 08 '14 at 11:31
  • I have read your post and I really dont know what you are trying to accomplish. You dont know how to pass const char* to function or what? – Dakorn Aug 08 '14 at 11:35
  • The string you are building in your function does not exist after the function returns, so the return value points to invalid memory. Return a `string` instead of a `char *` or allocate space for the `char *`. – nwp Aug 08 '14 at 11:36
  • so finally... somehow it did not want to accept my code – newandlost Aug 08 '14 at 11:37
  • `string s` is a local variable. I wouldn't be so sure that `return s.c_str()` is such a good idea, as it sounds to me like it's valid only during the execution of the function. – barak manos Aug 08 '14 at 11:45
  • 1
    possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – πάντα ῥεῖ Aug 08 '14 at 13:41

4 Answers4

2

why don't you return a string from the function instead of char*? like:

const string doubleToStr(double x){
  stringstream ss;
  ss << x;
  return ss.str();
}

R = 10;
string s;
s = doubleToStr(R);
cout << s << endl;

and if you really need a char*, you can use 's.c_str()' after the code above

glezmen
  • 554
  • 2
  • 5
  • I think it is not right answer for his question. He want to get `const char*` as return type so he is expecting such solution. – Dakorn Aug 08 '14 at 11:47
  • jap I want a const char * back – newandlost Aug 08 '14 at 11:56
  • if you _really_ want a const char*, then you can allocate the memory for it in the converter function and free it from the caller (and that's C, not C++) – glezmen Aug 08 '14 at 11:59
  • @newandlost: There's no (sensible) way to do that. Just return a `string`, and let the caller use `c_str()` if they really need a pointer for some reason. (The alternatives are a static array, which will be unexpectedly overwritten on the next call, or a dynamic array, which is bound to lead to memory leaks. Don't do that if you value your sanity.) – Mike Seymour Aug 08 '14 at 12:16
  • You shouldn't make the return type `const`; that prevents move semantics when you assign it to `s`. – Mike Seymour Aug 08 '14 at 12:18
  • @newandlost The `const char*` can always be obtained by just using `std::string::c_str()`. – πάντα ῥεῖ Aug 08 '14 at 13:44
  • ja that was the best solution. Then I don't even have to create a LPCSTR, when using .c_str() like `CreateWindow("STATIC", stringR.c_str(), WS_VISIBLE |WS_CHILD | SS_RIGHT, xSC, top, bS, hEaS, hwnd, NULL, hInstance, NULL);` – newandlost Aug 10 '14 at 09:07
2

Variable string s is a local variable in function doubleToLPSTR.

During runtime, once you're "out of" this function, this variable is destroyed.

So with return s.c_str(), you are essentially invoking undefined behavior.

The outcome of any attempt to access the returned address would be inconsistent.

barak manos
  • 29,648
  • 10
  • 62
  • 114
0

By the looks of it, the return value doesn't really need to be a char* - you are just outputting the return value after all - which means that you could use a string. (That is, of course there is a reason it needs to be a char* that you have omitted for brevity?)

Rather than having to convert the double to a string yourself then you can use the String class which has a method "std::to_string()" which can convert all numeric types, including double.

More information on that can be found here.

But the basic usage in your case would be...

R = 10;
std::cout << std::to_string(R) <<std::endl;
Carl Burnett
  • 171
  • 1
  • 8
0
const char * doubleToLPSTR(double x){

const int size = 20;
char *cs = new char[size];

string s;
stringstream ss;
ss << x;
s = ss.str();
const char * tempAr = s.c_str();


for (int i = 0; i < size; i++){

    cs[i] = tempAr[i];

}

return cs;
}

And then in the program:

R = 10;
const char *cs = doubleToLPSTR(R);
cout << cs << endl;

And I needed it because i nee a LPCSTR in

CreateWindow("STATIC", cs, WS_VISIBLE | WS_CHILD | SS_RIGHT, xSC, top, bS, hEaS,
    hwnd, NULL, hInstance, NULL);
newandlost
  • 935
  • 2
  • 10
  • 21