Title: Constant Pointer:
1(No Transcript)
2- Constant Pointer
- Has to be initialized when it is declared.
- After it is initialized, this pointer can not be
- redirected (i.e., a constant pointer cannot be
- assigned a value), but the variable that this
- pointer points to can be modified.
- Can be assigned to an ordinary pointer.
- Pointer to Constant
- May point to a non-constant variable, but this
- variable cannot be modified through this
- pointer. For instance, let p be a pointer to
- const int, and it points to an int variable x.
- Then x is, of course, not a constant, but p
- is a constant, although they refer to the
- same memory location.
- An ordinary pointer can be assigned to a pointer
- to constant, but a pointer to constant cannot
- be assigned to an ordinary pointer.
Examples pc y pc pp // OK, int is
converted to const int pp x // invalid,
cannot convert const int to int pp pc //
invalid, cannot convert const int to int pp
cp // OK, int const is converted to int pc
y // invalid, assignment to constant
z 5 // OK, although it is pointed to by a
pointer to constant z x // OK, const int is
converted to int pc 5 // invalid, assignment
to constant cp x // OK, assignment to
pointee cp x // invalid, redirection of
constant pointer cpc x // invalid,
redirection of constant pointer cpc x //
invalid, assignment to constant