Prerequisite: Pointers
There is a lot of confusion when char, const, *, p are all used in different permutaions and meanings change according to which is placed where. Following article focus on differentiation and usage of all of these.
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed. const keyword applies to whatever is immediately to its left. If there is nothing to its left, it applies to whatever is immediately to its right.
- const char *ptr : This is a pointer to a constant character. You cannot change the value pointed by ptr, but you can change the pointer itself. “const char *” is a (non-const) pointer to a const char.
// C program to illustrate// char const *p#include<stdio.h>#include<stdlib.h>intmain(){chara ='A', b ='B';constchar*ptr = &a;//*ptr = b; illegal statement (assignment of read-only location *ptr)// ptr can be changedprintf("value pointed to by ptr: %c\n", *ptr);ptr = &b;printf("value pointed to by ptr: %c\n", *ptr);}chevron_rightfilter_noneOutput:
value pointed to by ptr:A value pointed to by ptr:B
NOTE: There is no difference between const char *p and char const *p as both are pointer to a const char and position of ‘*'(asterik) is also same.
- char *const ptr : This is a constant pointer to non-constant character. You cannot change the pointer p, but can change the value pointed by ptr.
// C program to illustrate// char* const p#include<stdio.h>#include<stdlib.h>intmain(){chara ='A', b ='B';char*constptr = &a;printf("Value pointed to by ptr: %c\n", *ptr);printf("Address ptr is pointing to: %d\n\n", ptr);//ptr = &b; illegal statement (assignment of read-only variable ptr)// changing the value at the address ptr is pointing to*ptr = b;printf("Value pointed to by ptr: %c\n", *ptr);printf("Address ptr is pointing to: %d\n", ptr);}chevron_rightfilter_noneOutput:
Value pointed to by ptr: A Address ptr is pointing to: -1443150762 Value pointed to by ptr: B Address ptr is pointing to: -1443150762
NOTE: Pointer always points to same address, only the value at the location is changed.
-
const char * const ptr : This is a constant pointer to constant character. You can neither change the value pointed by ptr nor the pointer ptr.
// C program to illustrate//const char * const ptr#include<stdio.h>#include<stdlib.h>intmain(){chara ='A', b ='B';constchar*constptr = &a;printf("Value pointed to by ptr: %c\n", *ptr);printf("Address ptr is pointing to: %d\n\n", ptr);// ptr = &b; illegal statement (assignment of read-only variable ptr)// *ptr = b; illegal statement (assignment of read-only location *ptr)}chevron_rightfilter_noneOutput:
Value pointed to by ptr: A Address ptr is pointing to: -255095482
NOTE: char const * const ptr is same as const char *const ptr.
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between const int*, const int * const, and int const *
- What is the difference between "char a" and "char a[1]"?
- What's difference between char s[] and char *s in C?
- Diffference between #define and const in C?
- size of char datatype and char array in C
- char* vs std:string vs char[] in C++
- Function overloading and const keyword
- What is the difference between single quoted and double quoted declaration of char array?
- Const Qualifier in C
- Why copy constructor argument should be const in C++?
- Const member functions in C++
- C++ | const keyword | Question 1
- C++ | const keyword | Question 2
- C++ | const keyword | Question 3
- C++ | const keyword | Question 5
- C++ | const keyword | Question 5
- How to modify a const variable in C?
- “static const” vs “#define” vs “enum”
- Different ways to use Const with Reference to a Pointer in C++
- Const vs Regular iterators in C++ with examples
Improved By : Deepankar Mullick

