const char *p = "xxx" ; char *q = "zzz" ;
Remember that const objects must be initialized.
const int data1 ; // rejected const int data2 = 2 ; // fine const int *pdata1 ; // fine (pointed data is constant but pointer is not) int * const pdata2 ; // rejected (pointer is constant)
Here are some frequent usages of const with formal parameters.
void f(const char *p) { ++p ; // fine ++(*p) ; // rejected } void g(char * const p) { ++p ; // rejected ++(*p) ; // fine } void h(const char * const p) { ++p ; // rejected ++(*p) ; // rejected }