const
is useful
when declaring functions that take pointers or arrays as
arguments, but do not modify the dereferenced
contents:
int sum(const int *ar, int len) { int s = 0, i; for (i = 0; i < len; i++) s += ar[i]; return s; } int array[] = { 1, 2, 4, 5 }; int total = sum(array, 4);
The const
assures
the caller that the invocation will not attempt to assign
to *array
(or array[1]
, array[2]
, etc),
even though the elements of array
are modifiable in other
contexts.