Const 指针
单指针
- 
指向 int的指针指针可以指向不同的整数, int可以通过指针改变。这个代码示例指定 b 指向int b然后将b的值更改为100。int b; int* p; p = &b; /* OK */ *p = 100; /* OK */
- 
指向 const int的指针指针可以指向不同的整数,但 int的值不能通过指针改变。int b; const int* p; p = &b; /* OK */ *p = 100; /* Compiler Error */
- 
const指向int指针只能指向一个 int但是int的值可以通过指针改变。int a, b; int* const p = &b; /* OK as initialisation, no assignment */ *p = 100; /* OK */ p = &a; /* Compiler Error */
- 
const指向const int指针只能指向一个 int,而int不能通过指针改变。int a, b; const int* const p = &b; /* OK as initialisation, no assignment */ p = &a; /* Compiler Error */ *p = 100; /* Compiler Error */
指针指针
- 
指向 int指针的指针此代码将 p1的地址分配给双指针p(然后指向int* p1(指向int))。然后改变 p1指向int a。然后将 a 的值更改为 100。void f1(void) { int a, b; int *p1; int **p; p1 = &b; /* OK */ p = &p1; /* OK */ *p = &a; /* OK */ **p = 100; /* OK */ }
- 
指向 const int的指针void f2(void) { int b; const int *p1; const int **p; p = &p1; /* OK */ *p = &b; /* OK */ **p = 100; /* error: assignment of read-only location ‘**p’ */ }
- 
指向 const指向int的指针void f3(void) { int b; int *p1; int * const *p; p = &p1; /* OK */ *p = &b; /* error: assignment of read-only location ‘*p’ */ **p = 100; /* OK */ }
- 
const指向int的指针void f4(void) { int b; int *p1; int ** const p = &p1; /* OK as initialisation, not assignment */ p = &p1; /* error: assignment of read-only variable ‘p’ */ *p = &b; /* OK */ **p = 100; /* OK */ }
- 
指向 const的指针指向const intvoid f5(void) { int b; const int *p1; const int * const *p; p = &p1; /* OK */ *p = &b; /* error: assignment of read-only location ‘*p’ */ **p = 100; /* error: assignment of read-only location ‘**p’ */ }
- 
const指向const int的指针void f6(void) { int b; const int *p1; const int ** const p = &p1; /* OK as initialisation, not assignment */ p = &p1; /* error: assignment of read-only variable ‘p’ */ *p = &b; /* OK */ **p = 100; /* error: assignment of read-only location ‘**p’ */ }
- 
const指向const指向int的指针void f7(void) { int b; int *p1; int * const * const p = &p1; /* OK as initialisation, not assignment */ p = &p1; /* error: assignment of read-only variable ‘p’ */ *p = &b; /* error: assignment of read-only location ‘*p’ */ **p = 100; /* OK */ }