Am i correct?

Started by
4 comments, last by Zahlman 17 years, 8 months ago
Hello, Im finishing up my excercise at the end of my chapter in my c++ book, and im a little confused by the content of the excercises. Heres the content of the excercise and then below it, will be my code. Please tell me if I did in fact, replicate what my book excercise is appointing me to do. Note: I added in something of my own into the code. (nothing big) Note: There are no errors in my code. Excercise: "Write a program with a pointer to a pointer to a string object. Use the pointer to pointer to call the size() member function of the string object." My Source:

#include <string>
#include <iostream>

int main()
{
	std::string name = "Brandon";
	std::string* pName = &name;
	std::string* pPName = pName;

	std::cout << *pPName << "\n\n";
	for(int i = 0; i != pPName->size(); i++) {
		std::cout << "Character at position " << i << ": " << (*pPName) << std::endl;
	}

	return 0;
}


Advertisement
You are missing a level of indirection. pPName is merely a pointer to a string that has the same value as pName. pPName doesn't point to pName.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
You are missing a level of indirection. pPName is merely a pointer to a string that has the same value as pName. pPName doesn't point to pName.


How would i create the effect my book is asking for? Im still a little lost [sad]
Quote:Original post by NUCLEAR RABBIT
How would i create the effect my book is asking for? Im still a little lost [sad]


Take the same steps you took when you created pName and made it point to Name, but apply them to pName instead of Name. Then keep in mind that you need to go through two pointers to actually access Name from pPName.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by NUCLEAR RABBIT
How would i create the effect my book is asking for? Im still a little lost [sad]


Take the same steps you took when you created pName and made it point to Name, but apply them to pName instead of Name. Then keep in mind that you need to go through two pointers to actually access Name from pPName.


Sorry if this is wrong, but is this what you mean?

#include <string>#include <iostream>int main(){	std::string name = "Brandon";	std::string* pName = &name;	std::string* pPName = &(*pName);	std::cout << *pPName << "\n\n";	for(int i = 0; i != pPName->size(); i++) {		std::cout << "Character at position " << i << ": " << (*pPName) << std::endl;	}	return 0;}
For any type T, you can make the type pointer-to-T, which is T*.

Let T = string*. Then T* = string**.

So you need to declare a variable of that type.

You would then assign the address of some string* to the string**: that is, make the string** point at the string*.

Then, to access the string, you can dereference *twice*: once to get the pointer-to-string, and again to get the string.

This topic is closed to new replies.

Advertisement