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
#include<stdio.h>
#include<stdlib.h>
int main()
{
char a = 'A' , b = 'B' ;
const char *ptr = &a;
printf ( "value pointed to by ptr: %c\n" , *ptr);
ptr = &b;
printf ( "value pointed to by ptr: %c\n" , *ptr);
}
|
- 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
#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;
printf ( "Value pointed to by ptr: %c\n" , *ptr);
printf ( "Address ptr is pointing to: %d\n" , ptr);
}
|
Output:
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
#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);
}
|
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
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Sep, 2021
Like Article
Save Article