Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

Am i correct?

This topic is 4357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!