Const in C
A constant makes a value constant; the variable can't be reassigned or modified. Doing so will be a compilation error.
int main() {
const int i = 10;
i++; // error
}
The above code will not compile, as we are trying to mutate a constant variable.
int main() {
const int i = 10;
int j = 12;
//Pointer to a constant integer (Value cannot change, pointer can)
const int* const_ptr = &j;
//Constant pointer to a regular integer (Pointer cannot change, value can)
int* const const_val_ptr = &j;
//Both the pointer and the value it points to are constant
const int* const const_val_const_ptr = &j;
}
const int* ptr: The pointer can point to any other int, but can't mutate the int; i.e a read-only pointer.int* const ptr: The pointed-to value can be mutated, but it can't point to any other int.const int* const ptr: Neither the value can be mutated, nor can the pointer be updated to point to anything else. Both the pointer and the value are constant.
Function Argument Declarations
const can be used in function argument declaration.
int print(size_t size, const int arr[static size]);
The above function accepts a pointer to an array, but it can only read the values; it can't write to it.
Note: static size tells the compiler the array must have at least size elements and cannot be NULL.
