6 Replies - 409 Views - Last Post: 05 May 2012 - 02:41 PM Rate Topic: -----

#1 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 11-July 11

Exercise using pointers.

Posted 05 May 2012 - 01:49 PM

Hey all.

Today i got after reading up on pointers, i was presented with this assignment.

Exercise 1.

1. Write a program with a pointer to a pointer to a string object. Use
the pointer to the pointer to call the size() member function of
the string object.

I did a bit of work on it, but for some reason I'm not sure whether i did it right.

Here is what i did.

// Exercise 1

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string str = "Twigz";
	string* pStr = &str;

	cout << "*pStr is: " << *pStr << "\n";
	cout << "*pStr has a size of " << (*pStr).size() << " characters.\n";

	return 0;
}



Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Exercise using pointers.

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Exercise using pointers.

Posted 05 May 2012 - 02:06 PM

The assignment wants you to use a pointer to a pointer to a string. You only have a pointer to a string.

So you need one more level of indirection to fulfill the assignment, that is create a third variable that holds a pointer to pStr.
Was This Post Helpful? 1
  • +
  • -

#3 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 11-July 11

Re: Exercise using pointers.

Posted 05 May 2012 - 02:11 PM

Ok it sounds easy in words, but i'm not sure how i can go about implementing that.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Exercise using pointers.

Posted 05 May 2012 - 02:14 PM

Well, you already figured out how to create a pointer to str. Now you do the same thing again to pStr. And then when calling the size method, you need to dereference the pointer twice before you can call the method.
Was This Post Helpful? 1
  • +
  • -

#5 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 11-July 11

Re: Exercise using pointers.

Posted 05 May 2012 - 02:18 PM

Ok, with what you suggested. This is what i came up with.

// Exercise 1

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string str = "Twigz";
	string* pStr = &str;
	string** pTra = &pStr;

	cout << "**pTra is: " << **pTra << "\n";
	cout << "**pTra has a size of " << (**pTra).size() << " characters.\n";

	return 0;
}



What do you think?

This post has been edited by Twigz: 05 May 2012 - 02:20 PM

Was This Post Helpful? 1
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Exercise using pointers.

Posted 05 May 2012 - 02:36 PM

looks good, well done.

Just to note, C++ introduced the '->' operator for syntactical sugar when dereferencing pointers.

You could write the following line:
   cout << "**pTra has a size of " << (**pTra).size() << " characters.\n";



as
   cout << "**pTra has a size of " << (*pTra)->size() << " characters.\n";


Was This Post Helpful? 0
  • +
  • -

#7 Twigz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 11-July 11

Re: Exercise using pointers.

Posted 05 May 2012 - 02:41 PM

Thanks!

Quote

Just to note, C++ introduced the '->' operator for syntactical sugar when De-referencing pointers.


I know about that, it's just that I'm so used to using (.) instead of (->).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1