# Exercise using pointers.

Page 1 of 1

## 6 Replies - 510 Views - Last Post: 05 May 2012 - 02:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=278196&amp;s=04e5de342770782d8f58672bdd14a4de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Twigz

Reputation: 2
• Posts: 104
• 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

• D.I.C Lover

Reputation: 2271
• Posts: 3,486
• 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.

### #3 Twigz

Reputation: 2
• Posts: 104
• 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.

### #4 sepp2k

• D.I.C Lover

Reputation: 2271
• Posts: 3,486
• 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.

### #5 Twigz

Reputation: 2
• Posts: 104
• 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

### #6 jjl

• Engineer

Reputation: 1120
• Posts: 4,647
• 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";

```

### #7 Twigz

Reputation: 2
• Posts: 104
• 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 (->).