13,250,043 members (68,859 online)
alternative version

Stats

9.7K views
6 bookmarked
Posted 12 Jan 2010

Looking at pointer declarations

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

 Pro

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

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

Web02 | 2.8.171114.1 | Last Updated 13 Jan 2010