Click here to Skip to main content
Click here to Skip to main content

Looking at pointer declarations

By , 12 Jan 2010
 
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)

About the Author

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 So Simple But GoodmemberHeaven202020 Jun '11 - 4:31 

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

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