2. const T *p
1 and 2 are same. However, follow the convention of reading from the right to the left.
T const *p = p is a pointer to const T
p can be changed; however, *p can't be changed
Trick: move the decl-specifier "const" to the right, then read.
3. T *const p
p is a const pointer to T.
p can't be changed, however *p can be changed
4. T const *const p
5. const T *const p
4 &5 are same; but 5 is easy to read: p is a const pointer to const p.
Both p and *p can't be changed.
6. int x[N];
int const *p = x;
decl-specifier "const" is usually seen as a formal parameter;
int x[N];
int f(int const *p);
*p = 1; // error, const can't be changed.
x[0] = 1; //accepeted; x is unqualified
++p; //*p = x[1], accepted; p is unqualified
The latter trick is used to write to ROM (read Dan Sikes articles).
7. volatile vs const .
If there is no 'volatile', compiler optimizes and fucks up every thing. So, volatile forces no optimizations.
const and volatile: the device can change (volatile), but the program can't (const)
8. qualified types: when const/volatile
more specific: cv-qualified; const qualified; v qualified.
cv-unqualified
int i; //unqualified
int const j; //c-qualified int
int volatile k; //v-qualified int
int const *p; // *p is c-qualified; however, the ptr p is unqualified
9. const is a promise
10. qualification conversion: un-qualified >(to) qualified; cf. int > int const
int n = 0
int const *p = &n; //no error
int const m = 0
int *q = &m; //error (qual > unqual)
10.
int *p; // non-const pointer p to non const int.
11.
int m = 0;
int *const p = &m; // p points to the same storage area as m designates
int *q = p; //copies p, not an issue, does not change p
no promises violated
No comments:
Post a Comment