0

I want to assign integer to a char pointer using stringstream. But I am getting error while running this program at line ss >> p. Please help me here i want integer to go into the buffer first and the it must be assigned to a char*.

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream
using namespace std;
int main () 
{
    stringstream ss;
    int n=100;
    char *p;
    ss << n;
    ss >> p; //not working
    cout << ss;
    return 0;
}
  • 2
    You have a pointer, `p`, but where does it point? – Some programmer dude Jan 17 '17 at 13:25
  • Possible duplicate of [How to put stringstream contents into char type instead string type](http://stackoverflow.com/questions/8765574/how-to-put-stringstream-contents-into-char-type-instead-string-type) – hlscalon Jan 17 '17 at 13:27

2 Answers2

1

Use stringstream::str to get a C++ string, then use .c_str() on the string:

#include <string>       // std::string                                                                                                                                                                                                       
#include <iostream>     // std::cout                                                                                                                                                                                                         
#include <sstream>      // std::stringstream                                                                                                                                                                                                 
using namespace std;
int main ()
{
    stringstream ss;
    int n = 100;
    char* p;
    ss << n;

    string tmp = ss.str();
    p =  const_cast<char*>(tmp.c_str());

    cout << "p: " << p << '\n';

    return 0;
}

Beware that the char pointer becomes invalid as soon as the string goes out of scope. If you need some kind of factory function behavior, return a string by value, use strlcpy or maybe new and shared_ptr.

Erik Alapää
  • 2,585
  • 1
  • 14
  • 25
  • i didnt get it.please explain me briefly – prasad yalla Jan 17 '17 at 13:53
  • 1
    That `p` is invalid and your program has undefined behaviour. (And why did you cast away `const`?) – molbdnilo Jan 17 '17 at 14:12
  • @molbdnilo Normally, declaring the pointer as const char* would be better, I just wanted to change as little as possible from code in question. I cast away const to get the program to compile, and the output is correct. – Erik Alapää Jan 17 '17 at 14:28
  • 1
    @ErikAlapää When you print it, `p` points to an object that no longer exists. Welcome to C++, where getting the expected output does not imply that a program is correct, or even valid. – molbdnilo Jan 17 '17 at 14:37
  • @molbdnilo If you do not like C++, just do not use it. But OK, I polished the answer. – Erik Alapää Jan 17 '17 at 14:45
1
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream
using namespace std;

int main () 
{
    stringstream ss;
    int n=100;
    char buffer[100];
    char *p = buffer;
    ss << n;
    ss >> p;
    cout << p;
    return 0;
}

This is fixing only the problem you directly encountered - there's no storage behind p so it will crash. Stylistically there are many other things to improve / fix, but this should show you what part of this was actually wrong.

dascandy
  • 7,184
  • 1
  • 29
  • 50