×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pointers and typedefs

pointers and typedefs

pointers and typedefs

(OP)
Hi there,

I have been struggling for the last days trying to understand one statement made in the book I am reading at the moment (C++ Primer, 5th Ed, page 129)

It tries to explain pointers and typedefs and, for that, gives this bit of code:

typedef string *pstring;
const pstring cstr;

Saying (literally) then: "...what is the type of cstr? The simple answer is that it is a pointer to a const pstring...". After some explanation it concludes then that cstr is a const pointer to a string as well.

OK, I understand at the end cstr is a const pointer to a string (wasn't easy but finally got it), but honestly don't understand why it says it is a pointer to const pstring (note pstring and not strng)...why?? where does the pointer to a pstring come from??

If you could please help me I would be very pleased.
Thanks a lot indeed!!

RE: pointers and typedefs

> The simple answer is that it is a pointer to a const pstring
Simple, but wrong.

cstr is a const pstring
Within the declaration itself, there is no pointer, so saying that it's a "pointer to" is just wrong.

But then you have to look back to discover that pstring is really string*, at which point it becomes clear that cstr is a const string *

Sure, the fully resolved declaration is that cstr is a pointer to a const string.

> cstr is a const pointer to a string as well.
Careful, "const pointer" and "pointer to const" are both valid terms, but they don't mean the same thing.

CODE

char * const p = &mychar; // const pointer to (variable) char
*p = 'a';  // modifying what it points at is valid
p = &anotherchar; // modifying where it points is not.
vs.

CODE

const char *p = &mychar; // (variable) pointer to a const char
*p = 'a';  // modifying what it points at is invalid
p = &anotherchar; // modifying where it points is fine.
Equally, you can apply the 'const' qualifier to the "pointer" and the "pointee".
 

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close