char* f(){
char* result;//I have to return 'result' at the end of this function, where result should be containing i (i.e. int i=97) in it.
int i = 97;
//Do whatever you can
return result;
}
MRehanQadri
0
Junior Poster in Training
Recommended Answers
Jump to PostWhat's the purpose of that function, why do you need to convert the int to a string? In c++ you can use stringstream to make the conversion
#include <string> #include <sstream> using namespace std; std::string foo() { int i = 20; string n; stringstream str(i); str >> …
Jump to PostYou can get the char* from the string after it's returned.
char* s = string.c_str()
The only other option is to allocate new memory, then you have to make sure to delete it when no longer needed.
#include <cstdlib> char* foo() { int i = 20; …
Jump to PostThere is no stringstream constructor that takes a single parameter of type int. This function:
stringstream str(i);
does not exist.
All 10 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
MRehanQadri
0
Junior Poster in Training
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
MRehanQadri
0
Junior Poster in Training
Moschops
683
Practically a Master Poster
Featured Poster
MRehanQadri
0
Junior Poster in Training
Moschops
683
Practically a Master Poster
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Moschops
683
Practically a Master Poster
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.