Difference between const char *p, char * const p and const char * const p

11 Sep, 2021
Prerequisite: Pointers There is a lot of confusion when char, const, *, p are all used in different permutations 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. 
1. 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
// C program to illustrate// char const *p#include<stdio.h>#include<stdlib.h> int main(){    char a ='A', b ='B';    const char *ptr = &a;         //*ptr = b; illegal statement (assignment of read-only location *ptr)         // ptr can be changed    printf( "value pointed to by ptr: %c\n", *ptr);    ptr = &b;    printf( "value pointed to by ptr: %c\n", *ptr);}
Output: 
Output: 
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.  
2. 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
// C program to illustrate// char* const p#include<stdio.h>#include<stdlib.h> int main(){    char a ='A', b ='B';    char *const ptr = &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);}
Output: 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
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. 
3. 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
// C program to illustrate//const char * const ptr#include<stdio.h>#include<stdlib.h> int main(){    char a ='A', b ='B';    const char *const ptr = &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) }
Output: 
Value pointed to by ptr: A
Address ptr is pointing to: -255095482
NOTE: char const * const ptr is same as const char *const ptr. Quiz on const keyword
This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
Deepankar Mullick
adnanirshad158
C-Pointer Basics
cpp-pointer
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Priority Queue in C++ Standard Template Library (STL)
Set in C++ Standard Template Library (STL)