pointer to a string object

Hi everybody, I have an exercise that says: Write a program with a pointer to a string object. Use the pointer to the pointer to call the size() member function of the string object.
I did this but it doesn't work.

#include <iostream>
#include <string>

using namespace std;

int main()
{

cout << "Assigning &str to pStr\n";
string str = "score";
string* pStr = &str; //pointer to string object
cout << "str is: " << str << "\n";
cout << "*pStr is: " << *pStr << "\n";
cout << "(*pStr).size() is: " << (*pStr).size() << "\n";

return 0;

}
I need help, thank you in advance
Last edited on
How does it not work?
Though if the assignment says to "use the pointer to the pointer", you probably need another variable string** ppStr = &pStr;
What about now?.. the thing is in the chapter it never teaches about using **. There is another way?

#include <iostream>
#include <string>

using namespace std;

int main()
{
cout << "Assigning &str to pStr\n";
string str = "score";
string* pStr = &str; //pointer to string object
string** ppStr = &pStr;
cout << "str is: " << str << "\n";
cout << "*pStr is: " << *pStr << "\n";
cout << "(*pStr).size() is: " << (**ppStr).size() << "\n";

return 0;
}
If I had to guess, it meant to say "use the pointer to the string" rather than "use the pointer to the pointer".
it's what i understood! so what it's wrong, because, i submit the first exercise and I got 0 points.
I need hepl!
What didn't work in the original snippet?
I wish i know!...the teacher didn't give feedback!
Topic archived. No new replies allowed.