Examples of const

Examples

 1. void foo(void)
 2. {
 3.   int i = 5;          /* i is a non-constant int */
 4.   int j = 6;          /* j is a non-constant int */
 5.   const int ci = 10;  /* ci is a constant int    */
 6.   const int cj = 11;  /* cj is a constant int    */
 7.
 8.   int *pi;                      /* pi is a pointer to an int              */
 9.   const int *pci;               /* pci is a pointer to a const int        */
10.   int * const cpi = &i;         /* cpi is a const pointer to an int       */
11.   const int * const cpci = &ci; /* cpci is a const pointer to a const int */
12.
13.   i = 6;      /* Ok, i is not const    */
14.   j = 7;      /* Ok, j is not const    */
15.   ci = 8;     /* ERROR: ci is const    */
16.   cj = 9;     /* ERROR: cj is const    */
17.
18.   pi = &i;    /* Ok, pi is not const   */
19.   *pi = 8;    /* Ok, *pi is not const  */
20.   pi = &j;    /* Ok, pi is not const   */
21.   *pi = 9;    /* Ok, *pi is not const  */
22.
23.   pci = &ci;  /* Ok, pci is not const  */
24.   *pci = 8;   /* ERROR: *pci is const  */
25.   pci = &cj;  /* Ok, pci is not const  */
26.   *pci = 9;   /* ERROR: *pci is const  */
27.
28.   cpi = &j;   /* ERROR: cpi is const   */
29.   *cpi = 10;  /* Ok, *cpi is not const */
30.   *cpi = 11;  /* Ok, *cpi is not const */
31.
32.   cpci = &j;  /* ERROR: cpci is const  */
33.   *cpci = 10; /* ERROR: *cpci is const */
34.  
35.   pi = &ci;   /* DANGER: constant ci can be changed through pi */
36. }


This is the output from the gcc compiler (code is in temp.c):

temp.c: In function 'foo':
temp.c:15:3: error: assignment of read-only variable 'ci'
temp.c:16:3: error: assignment of read-only variable 'cj'
temp.c:24:3: error: assignment of read-only location '*pci'
temp.c:26:3: error: assignment of read-only location '*pci'
temp.c:28:3: error: assignment of read-only variable 'cpi'
temp.c:32:3: error: assignment of read-only variable 'cpci'
temp.c:33:3: error: assignment of read-only location '*cpci'
temp.c:35:6: warning: assignment discards 'const' qualifier from pointer target type [enabled by default]


This is the output from Microsoft's C compiler:

temp.c
temp.c(15) : error C2166: l-value specifies const object
temp.c(16) : error C2166: l-value specifies const object
temp.c(24) : error C2166: l-value specifies const object
temp.c(26) : error C2166: l-value specifies const object
temp.c(28) : error C2166: l-value specifies const object
temp.c(32) : error C2166: l-value specifies const object
temp.c(33) : error C2166: l-value specifies const object
temp.c(35) : warning C4090: '=' : different 'const' qualifiers


This is from Embarcadero's (formerly Borland) C compiler:

temp.c:
Error E2024 temp.c 15: Cannot modify a const object in function foo
Error E2024 temp.c 16: Cannot modify a const object in function foo
Error E2024 temp.c 24: Cannot modify a const object in function foo
Error E2024 temp.c 26: Cannot modify a const object in function foo
Error E2024 temp.c 28: Cannot modify a const object in function foo
Error E2024 temp.c 32: Cannot modify a const object in function foo
Error E2024 temp.c 33: Cannot modify a const object in function foo
Warning W8075 temp.c 35: Suspicious pointer conversion in function foo
Warning W8004 temp.c 36: 'cpci' is assigned a value that is never used in function foo
Warning W8004 temp.c 36: 'pci' is assigned a value that is never used in function foo
*** 7 errors in Compile ***


This is from the Clang compiler:

temp.c:15:6: error: read-only variable is not assignable
  ci = 8;     /* ERROR: ci is const    */
  ~~ ^
temp.c:16:6: error: read-only variable is not assignable
  cj = 9;     /* ERROR: cj is const    */
  ~~ ^
temp.c:24:8: error: read-only variable is not assignable
  *pci = 8;   /* ERROR: *pci is const  */
  ~~~~ ^
temp.c:26:8: error: read-only variable is not assignable
  *pci = 9;   /* ERROR: *pci is const  */
  ~~~~ ^
temp.c:28:7: error: read-only variable is not assignable
  cpi = &j;   /* ERROR: cpi is const   */
  ~~~ ^
temp.c:32:8: error: read-only variable is not assignable
  cpci = &j;  /* ERROR: cpci is const  */
  ~~~~ ^
temp.c:33:9: error: read-only variable is not assignable
  *cpci = 10; /* ERROR: *cpci is const */
  ~~~~~ ^
temp.c:35:6: warning: assigning to 'int *' from 'const int *' discards qualifiers [-Wincompatible-pointer-types]
  pi = &ci;   /* DANGER: constant ci can be changed through pi */
     ^ ~~~
1 warning and 7 errors generated.