To hold the address of a const variable we need a const pointer.
const int x = 5;
const int *p;
p = &x;
Example:
const int x = 5; int *p = &x;
The above results an error results while compilation showing an error that
"cannot convert from 'const int *' to 'int *' ". Because x is declare as
“const int” and to hold the address of x in a pointer we must have to declare
the pointer as const pointer (const int *p).
const int x = 10;
const int *p = &x;
*p = 7;
Error during compilation, because x is constant. And it is not possible to
alter the value of x by using a pointer *p = 7.
int x = 5;
const int *p;
p = &x;
*p = 7;
It is not possible to change the value of x using pointer p because pointer p
is declare as constant. The above program display an error "l-value specifies const object" while compiling the program.
Saturday, May 29, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment