pointer to a string object

Nov 2, 2011 at 4:13am
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 Nov 2, 2011 at 4:14am
Nov 2, 2011 at 6:38am
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;
Nov 2, 2011 at 10:42pm
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;
}
Nov 2, 2011 at 10:47pm
If I had to guess, it meant to say "use the pointer to the string" rather than "use the pointer to the pointer".
Nov 2, 2011 at 10:55pm
it's what i understood! so what it's wrong, because, i submit the first exercise and I got 0 points.
I need hepl!
Nov 3, 2011 at 1:59am
What didn't work in the original snippet?
Nov 3, 2011 at 5:01am
I wish i know!...the teacher didn't give feedback!
Topic archived. No new replies allowed.