//const справа от * фиксирует указатель,
//слева от * - значение
int a = 1;
int b = 2;
//указатель на const: менять значение через него нельзя
const int *pValue = &a;
//*pValue = 10; //<-Error
pValue = &b;
//*pValue is 2
//const-указатель: он не может указывать на другую переменную
int *const pFixed = &a;
*pFixed = 10;
//pFixed = &b; //<-Error
//a is 10
//зафиксировано и то, и другое
const int *const pBoth = &b;
//*pBoth is 2
//*pBoth = 20; //<-Error
//pBoth = &a; //<-Error
//const в параметре: функция обещает только читать
int sum(const int *numbers, size_t count) {
int total = 0;
for (size_t i = 0; i < count; i++) {
total += numbers[i];
}
return total;
}
int numbers[] = {1, 2, 3};
int total = sum(numbers, 3);
//total is 6
printf("*pValue is %d\n", *pValue);
printf("a is %d\n", a);
printf("*pBoth is %d\n", *pBoth);
printf("total is %d\n", total);