Here are the Difference between const char *p, char * const p and const char * const p coding

Prerequisite: Pointers
There is quite a lot of confusion when char, const, *, p are all utilized in completely different permutations and meanings change in accordance with which is positioned the place. Following article give attention to differentiation and utilization of all of those.
The qualifier const will be utilized to the declaration of any variable to specify that its worth is not going to be modified. const key phrase applies to no matter is straight away to its left. If there may be nothing to its left, it applies to no matter is straight away to its proper.

1. const char *ptr : This is a pointer to a continuing character. You can not change the worth pointed by ptr, however you’ll be able to change the pointer itself. “const char *” is a (non-const) pointer to a const char.

Take a step-up from these “Hello World” applications. Learn to implement data constructions like Heap, Stacks, Linked List and lots of extra! Check out our Data Structures in C course to begin studying at the moment.

C

#include<stdio.h>

#include<stdlib.h>

int main()

{

    char a ='A', b ='B';

    const char *ptr = &a;

    

    

    

    

    printf( "value pointed to by ptr: %cn", *ptr);

    ptr = &b;

    printf( "value pointed to by ptr: %cn", *ptr);

}

  1. Output:
worth pointed to by ptr:A
worth pointed to by ptr:B

NOTE: There isn’t any distinction between const char *p and char const *p as each are pointer to a const char and place of ‘*'(asterik) can be identical.
2. char *const ptr : This is a continuing pointer to non-constant character. You can not change the pointer p, however can change the worth 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: %cn", *ptr);

    printf( "Address ptr is pointing to: %dnn", ptr);

    

    

    *ptr = b;

    printf( "Value pointed to by ptr: %cn", *ptr);

    printf( "Address ptr is pointing to: %dn", 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 all the time factors to identical tackle, solely the worth on the location is modified.

3. const char * const ptr : This is a continuing pointer to fixed character. You can neither change the worth 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: %cn", *ptr);

    printf( "Address ptr is pointing to: %dnn", ptr);

    

    

}

Output:

Value pointed to by ptr: A
Address ptr is pointing to: -255095482

NOTE: char const * const ptr is identical as const char *const ptr.
Quiz on const key phrase

This article is contributed by Yash Singla. If you want GeeksforGeeks and wish to contribute, you too can write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article showing on the GeeksforGeeks main web page and assist different Geeks.
Please write feedback should you discover something incorrect, otherwise you need to share extra details about the subject mentioned above.

Add a Comment

Your email address will not be published. Required fields are marked *