Click here to Skip to main content
12,397,244 members (63,377 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8.5K views
6 bookmarked
Posted

Looking at pointer declarations

, 12 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
When we look at pointer declarations, reading from right-to-left gives a better idea about what the pointer actually points to. Consider const int * p; if we read right to left: p -> * -> int -> const. i.e. 'p is a pointer to an integer constant' rather than 'constant integer pointer' (if we...
When we look at pointer declarations, reading from right-to-left gives a better idea about what the pointer actually points to.

Consider const int * p; if we read right to left: p -> * -> int -> const. i.e. 'p is a pointer to an integer constant' rather than 'constant integer pointer' (if we read left to right) which doesn't give an idea if the pointer is constant or the value it is pointing to.

So reading it right to left gives clear idea that in the above declaration, p can be modified (like p++) but the value p is pointing to (*p) cannot be modified as it is pointing to a constant.

Let us consider another example int * const p;. Again let us use the same right-to-left trick, p -> const ->* -> int. i.e. p is a constant pointer to an integer. In this case, p cannot be modified (like p++) but the value it is pointing to can be modified (like *p = 2).

Finally in const int * const p; 'p is a const pointer to an integer constant' by using the same RTL trick.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rejeesh.T.S
Technical Lead
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 So Simple But Good Pin
Heaven202020-Jun-11 4:31
memberHeaven202020-Jun-11 4:31 
Generalquiet different thinking.. :) Pin
Cool_Dev3-Aug-10 4:54
memberCool_Dev3-Aug-10 4:54 
GeneralReason for my vote of 5 quiet different way of thinking.. Pin
Cool_Dev3-Aug-10 4:52
memberCool_Dev3-Aug-10 4:52 
Reason for my vote of 5
quiet different way of thinking..
Generalnice! Pin
Sir Gizzle10-Feb-10 22:43
memberSir Gizzle10-Feb-10 22:43 
Generalfor details look at Pin
Manish K. Agarwal10-Feb-10 19:53
memberManish K. Agarwal10-Feb-10 19:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 13 Jan 2010
Article Copyright 2010 by Rejeesh.T.S
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid