
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Difference between const char* p, char * const p, and const char * const p in C
Pointer
In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer.
const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed. But we can change the value of pointer as it is not constant and it can point to another constant char.
char* const says that the pointer can point to a char and value of char pointed by this pointer can be changed. But we cannot change the value of pointer as it is now constant and it cannot point to another char.
const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.
Thumb rule is to naming syntax from right to left.
// constant pointer to constant char const char * const // constant pointer to char char * const // pointer to constant char const char *
Example (C)
Uncomment the commented errorneous codes and see the error.
#include <stdio.h> int main() { //Example: char const* //Note: char const* is same as const char* const char p = 'A'; // q is a pointer to const char char const* q = &p; //Invalid asssignment // value of p cannot be changed // error: assignment of read-only location '*q' //*q = 'B'; const char r = 'C'; //q can point to another const char q = &r; printf("%c
", *q); //Example: char* const char u = 'D'; char * const t = &u; //You can change the value *t = 'E'; printf("%c", *t); // Invalid asssignment // t cannot be changed // error: assignment of read-only variable 't' //t = &r; //Example: char const* const char const* const s = &p; // Invalid asssignment // value of s cannot be changed // error: assignment of read-only location '*s' // *s = 'D'; // Invalid asssignment // s cannot be changed // error: assignment of read-only variable 's' // s = &r; return 0; }
Output
C E
- Related Articles
- Difference between const int*, const int * const, and int const * in C
- Difference between const int*, const int * const, and int const * in C/C++?
- How to convert an std::string to const char* or char* in C++?
- How to convert a std::string to const char* or char* in C++?
- What is the difference between const int*, const int * const, and int const *?
- Difference between #define and const in C
- Difference between readonly and const keyword in C#
- Difference between #define and const in Arduino
- Difference Between Static and Const in JavaScript
- JavaScript Const
- Const Qualifier in C
- Const cast in C++
- Explain the difference between const and readonly keywords in C#
- Const member functions in C++
- What is difference between int and const int& in C/C++?
