Home

C const cheatsheet

Constant type
int const n = 4;
n = 3; /* Error */
Constant array
/* Constant array of integers */
int const array[] = {3, 4};
array[0] = 5; /* Error */
Pointer to a constant type
/* Pointer to a constant character */
char const *str = "hello";
str++; /* Fine */
*str = 'p'; /* Error */
Constant pointer
char s_array[] = "porld";
/* Constant pointer to a character */
char * const p = s_array;
*p = 'w'; /* Fine */
p++; /* Error */
Constant pointer to a constant type
/* Constant pointer to a constant character */
char const * const str = "hello";
str++; /* Error */
*str = 'p'; /* Error */