What does const int do in C?
Table of Contents
What does const int do in C?
const int* const says that the pointer can point to a constant int and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant int.
What does const int * mean?
pointer to constant integer
int const* is pointer to constant integer. This means that the variable being declared is a pointer, pointing to a constant integer. Effectively, this implies that the pointer is pointing to a value that shouldn’t be changed.
Can you use const in C?
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change the value of const variable by using pointer ).
Is const int the same as int const?
Yes, they are the same. The rule in C++ is essentially that const applies to the type to its left.
What do you understand from this const int * ptr?
int * const ptr is a constant pointer to an integer. int const * ptr is the same thing as const int * ptr which means it is a pointer to a constant integer. In this case, you can change which constant integer variable the pointer is pointing to, but you cannot change the value of that integer variable.
What is the difference between const int and int const in C?
So in your question, “int const *” means that the int is constant, while “int * const” would mean that the pointer is constant. If someone decides to put it at the very front (eg: “const int *”), as a special exception in that case it applies to the thing after it.
Why should I use const?
const only prevents re-assigning, it doesn’t make the entire object immutable. It’s useful to use const instead of let , because it prevents you from accidentally overwriting variables. So a good rule of thumb is: Stop using var .
Why should we use const?
Why we use const most of the time? Because the const declaration creates a read-only reference to a value (cannot be reassigned). It prevents us to be confused with the value of the variable.