const qualifier

The const qualifier is implemented, as specified by the ANSI standard. However, text literals like "Hello" are not considered as arrays of const char (as they should be), but as arrays of char. This is consistent with most existing C compilers. For this reason, the following definitions are both correct, but the first one is obviously preferable.
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
}



Alain Gibaud 2015-07-09