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

Looking at pointer declarations

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

About the Author

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

Comments and Discussions

 
GeneralReason for my vote of 5 So Simple But Good PinmemberHeaven202020-Jun-11 4:31 
Generalquiet different thinking.. :) PinmemberCool_Dev3-Aug-10 4:54 
quiet different thinking.. Smile | :)
GeneralReason for my vote of 5 quiet different way of thinking.. PinmemberCool_Dev3-Aug-10 4:52 
Generalnice! PinmemberSir Gizzle10-Feb-10 22:43 
Generalfor details look at PinmemberManish K. Agarwal10-Feb-10 19:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140709.1 | Last Updated 13 Jan 2010
Article Copyright 2010 by Rejeesh.T.S
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid