|
#include <iostream>
#include <string>
int main(void) {
std::string str;
std::cout << "Please enter string:" << std::endl;
std::cin >> str;
std::cout << str << std::endl;
return 0;
}
|
It works fine.
Now I want to experiment with pointer to string.
|
#include <iostream>
#include <string>
int main(void) {
std::string* pstr;
std::cout << "Please enter string:" << std::endl;
std::cin >> *pstr;
std::cout << *pstr << std::endl;
return 0;
}
|
It compiles and runs.
But after I type something and enter, the error message appears
Segmentation fault (core dumped) ./a.out
I have no idea why.
Why can't std::string* pstr; be used to hold a string in C++ while only std::string str; can?
No comments:
Post a Comment