What is Const Pointer in C

What is Const Pointer in C
Techiio-author
Written by Shuvhojit DebFebruary 6, 2022
8 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this article, you'll know about constant pointers in C.

Constant Pointers

A constant pointer in C cannot change the address of the variable to which it is pointing, i.e., the address will remain constant. Therefore, we can say that if a constant pointer is pointing to some variable, then it cannot point to any other variable.

Syntax

<type of pointer> *const <name of pointer>;  

The Declaration of a constant pointer is given below:

int *const ptr;  

How did Const Pointers Work in C?

We can explain the working of the constant pointer in the following way,

  • const int * ptr, Then writing the const means we're informing the compiler about the ptr variable. We're informing them that this is a variable that will hold the address of a variable integer.
  • If we try to change the value refocused by the pointer it'll throw an error. Simply we can't change the value refocused by it but we can change the pointer.
  • To change the pointer itself we can assign it in this way “ ptr = & variable1”. Then we're changing the pointer itself.
  • Still, it'll not work as we're trying to change the value refocused by the pointer, If we try to write it * ptr = variable1.
  • To produce 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 housekeeper, int, pier according to our demand.
  • The alternate important trait of any constant pointer is the keyword const. This trait is used to inform the C compiler about the variable geste which we're going to use in the program. This informs the compiler that whatever variable address it'll hold. And it notifies the C compiler that whatever it'll hold will remain the same for the rest of the program. In case we try to change the pointer value it'll throw an error.
  • A third important trait of the constant pointer is the Any name of pointer. This trait is the name of the pointer which is going to point to the variable address. If we publish the printer “ * pointer” name will show us the address of the pointing variable.
  • A fourth important trait of the constant pointer is the *. Then * represents the value of the variable held by the pointer.

Examples of Const Pointer in C

Here are the following examples mentioned 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 the next after changing the allocation of the pointer.
  • In the last print, we can see the pointer is pointing to 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:

blogpost

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:

blogpost

Conclusion

From this tutorial, we learned about the const pointers in C programming and we learned about the const pointer’s actions with some important exemplifications. We learned the places where These const pointers can be used in real life with the help of some important exemplifications.

C Language
Programming
Constant Pointer
C Constants
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance