Updated April 13, 2023
Introduction to Const Pointer in C
The constant pointers in the C language are the pointers which hold the address of any variable and value of these constant pointers can not change once assigned, in the more technical word if any pointer is pointing to the memory address of a variable and it will not allow us to change the pointer memory allocation to other memory location, these kinds of stuff we used in case if we want the address of any variable to be fixed or we do not want to allocate any other memory location to a pointer, we can set the data type of cons pointer they can be integer float, etc according to our requirement or data type of variable which cons pointer is pointing. In this topic, we are going to learn about Const Pointer in C.
Syntax
Below is the syntax for the constant pointers in the C, we can explain the syntax in the following steps.
- Data type of pointer: The part is all about the data type of the variable which we are going to hold. We can define char, int, float according to our requirement.
- const: This attribute is used to inform the C compiler about the variable behavior which we are going to use in the program. This informs the compiler that whatever variable address it will hold, will remain the same for the rest of the program. In case we try to change the memory address, which means assigning addresses of other variables instead of the older one.
- Any name of pointer: This is the name of the pointer which is going to hold the address of the variable. If we print the printer we can see the output of holding the variable address.
- * : Here * represent the value of variable hold by the pointer.
This the syntax for declaration of constant pointers.
<Data type of pointer> * const <Any name of pointer>
This is the syntax for the definition of the constant pointers.
const <Data type of pointer>* <Any name of pointer>
How did Const Pointers Work in C?
We can explain the working of the constant pointer in the following steps,
- const int *ptr, Here writing the const means we are informing the compiler about the ptr variable. We are informing them that this is a variable that will hold the address of a variable integer.
- If we try to change the value pointed by the pointer it will throw an error. Simply we can not change the value pointed by it but we can change the pointer.
- To change the pointer itself we can assign it in this way “ ptr = &variable1”. Here we are changing the pointer itself.
- If we try to write it *ptr=variable1, it will not work as we are trying to change the value pointed by the pointer.
- To create any constant pointer the first thing which we need is the data type of the pointer. This informs the C compiler about the data type of the variable which pointer is going to hold. We can define char, int, float according to our requirement.
- The second important attribute of any constant pointer is the keyword const. This attribute is used to inform the C compiler about the variable behavior which we are going to use in the program. This informs the compiler that whatever variable address it will hold. And it notifies the C compiler that whatever it will hold will remain the same for the rest of the program. In case we try to change the pointer value it will throw an error.
- A third important attribute of the constant pointer is the Any name of pointer. This attribute is the name of the pointer which is going to point to the variable address. If we print the printer “* pointer” name will show us the address of the pointing variable.
- A fourth important attribute of the constant pointer is the *. Here * represent the value of variable hold by the pointer.
Examples of Const Pointer in C
Here are the following examples mention below:
Example #1
In this example, we are defining a constant pointer and changing the variable pointed by it.
- We are writing code inside the main, where we have defined a few variables like variable1 and variable2 and we defined a const char pointer.
- The variable1 address will be pointed by the const char pointer variable.
- Finally, we are printing data into two formats, one with a pointer holding the address of the variable1 and next after changing the allocation of the pointer.
- In the last print, we can see the pointer is pointing to the variable2.
Please see the below example along with the screen of output.
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char variable1 ='X', variable2 ='Y';
const char *pointer = &variable1;
//*pointer = variable2; This is not the correct way to change the pointer value , it will throw an error.
// Still we can change the pointer by changing the pointer itself
printf( "Current value of the pointer is : %c\n", *pointer);
pointer = &variable2;
printf( "The value of the pointer after change is : %c\n", *pointer);
}
Output:
Example #2
In this example, we are shown how we are changing the variable pointed by the pointer and even after changing the variable pointed by it still the pointer holds the same memory address.
Please see the below example along with the screen of output.
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char variable1 ='X', variable2 ='Y';
char *const pointer = &variable1;
//*pointer = variable2; This is not the correct way to change the pointer address , it will throw an error.
// Still we can change the pointer by changing the pointer itself
printf( "The value pointed by the pointer is : %c\n", *pointer);
printf( "The address pointed by the char pointer is : %d\n\n", pointer);
// We are only changing the variable point by pointer it address still same
*pointer = variable2;
printf( "The value pointed by the pointer is : %c\n", *pointer);
printf( "The address pointed by the char pointer is : %d\n", pointer);
}
Output:
Conclusion
From this tutorial, we learned about the const pointers in C programming and we learned about the const pointer’s behaviors with some important examples. We learned the places where These const pointers can be used in real life with the help of some important examples.
Recommended Articles
This is a guide to Const Pointer in C. Here we discuss how did Const Pointers work in C with respective programming examples to understand better. You may also have a look at the following articles to learn more –