Introduction
The programming language C is procedural. Dennis Ritchie created it for the first time in 1972. It was primarily developed as a programming language for creating operating systems. Low-level memory access, a minimal set of keywords, and a clean style are among the properties that make the C language suited for system programs, such as operating systems or compiler development. In this blog, we will learn about Const Qualifier in C.
Also See, Sum of Digits in C, C Static Function
Read more, odd or even program in c
Const Qualifier in C
The Const Qualifier is used to specify a variable as a constant, which means that its value will not change after it is initialised. However, there are some advantages to using const, such as the fact that if the PI has a constant value, we don't want any part of the program to change it. As a result, we may use the const qualifier to declare it as a const. However, whether we can change the value of the variable declared using Const Qualifier or not depends on where the const variables are stored in the memory.
Pointer to constant
Declaration
const int *ptr;
// or
int const *ptr;
Here 'ptr' is pointing to a const int. So here, we can change the Pointer to point to some other variable but cannot change the value of the pointed variable.
Example
#include <stdio.h>
// Pointer to constant.
void pointer_to_constant(){
/* The value of the object (entity) pointed by pointer ptr cannot be
changed. The read-write area stores the pointer (stack in the present case). The pointed object could be in a read-only or read-write area. */
int i = 10;
int j = 20;
const int *ptr = &i; /* ptr is pointer to constant */
printf("ptr: %d\n", *ptr);
*ptr = 100; /* error: object pointed cannot be modified
using the pointer ptr */
ptr = &j; /* valid */
printf("ptr: %d\n", *ptr);
}
int main(void){
pointer_to_constant();
return 0;
}
Output
cf.cpp: In function ‘void pointer_to_constant()’:
cf.cpp:18:10: error: assignment of read-only location ‘* ptr’
18 | *ptr = 100; // error: object pointed cannot be modified
| ~~~~~^~~~~
You can also read about the jump statement, Tribonacci Series
Must Read Passing Arrays to Function in C
Constant Pointer to constant
Declaration
const int *const ptr;
Here a constant pointer is pointing to a constant integer. So here, neither we can change the Pointer to point to any other variable, nor we can change the value of the variable being pointed.
Example
#include <stdio.h>
// constant pointer to constant
void constantPointer_to_constant(){
/* We can't change the value pointed by the pointer or point it to other
variables because it's a constant pointer to a constant variable. */
int i = 10;
int j = 20;
const int *const ptr = &i; /* constant pointer to constant integer */
printf("ptr: %d\n", *ptr);
ptr = &j; /* error */
*ptr = 100; /* error */
}
int main(void){
constantPointer_to_constant();
return 0;
}
Output
cf.cpp: In function ‘void constantPointer_to_constant()’:
cf.cpp:15:9: error: assignment of read-only variable ‘ptr’
15 | ptr = &j; /* error */
| ~~~~^~~~
cf.cpp:16:10: error: assignment of read-only location ‘*(const int*)ptr’
16 | *ptr = 100; /* error */
| ~~~~~^~~~~
Also see, Short int in C Programming
Must Read Decision Making in C