12,397,244 members (63,377 online)
alternative version

8.5K views
6 bookmarked
Posted

# Looking at pointer declarations

, 12 Jan 2010 CPOL
 Rate this:
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.

## Share

No Biography provided

## You may also be interested in...

 First Prev Next
 Reason for my vote of 5 So Simple But Good Heaven202020-Jun-11 4:31 Heaven2020 20-Jun-11 4:31
 quiet different thinking.. :) Cool_Dev3-Aug-10 4:54 Cool_Dev 3-Aug-10 4:54
 Reason for my vote of 5 quiet different way of thinking.. Cool_Dev3-Aug-10 4:52 Cool_Dev 3-Aug-10 4:52
 Reason for my vote of 5 quiet different way of thinking..
 nice! Sir Gizzle10-Feb-10 22:43 Sir Gizzle 10-Feb-10 22:43
 for details look at Manish K. Agarwal10-Feb-10 19:53 Manish K. Agarwal 10-Feb-10 19:53
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Jul-16 23:43 Refresh 1