May 2012
Intermediate to advanced
679 pages
16h 56m
English
In the fight against inadvertent errors, the keyword const plays a great role. We have seen it in action with simple variables earlier. We also know its use in function definitions. Now let us see it in action in different situations.
Keyword const can be used with pointers too. We can study three different situations.
Pointer to constant points to a constant. We may declare pointer ptr as
const int *ptr = & j ;
In this declaration, const gets attached to int (not to pointer). Hence, the protection is applied to variable j as referred by *ptr. The compiler does not allow us to change *ptr.
Note the following:
Compiler allows us to change the ptr.
Compiler allows us to change j. (but not *ptr ...
Read now
Unlock full access