const*和*const
References:
The data types of const char * and char * const in C language
In C, const char *
and char * const
are two different pointer types with different meanings regarding the mutability of the pointer and the data it points to. Let's break them down:
1. const char *
(Pointer to constant character)
- This means the pointer can be changed, but the data it points to cannot be modified.
- The data is constant (read-only), but the pointer itself can be reassigned to point to different data.
Example:
const char *ptr = "Hello";
ptr = "World"; // OK: You can change the pointer to point to another string.
*ptr = 'A'; // Error: You cannot change the content of what the pointer points to.
const char *
is often used for string literals and other data that should not be modified, but the pointer can still be reassigned.
2. char * const
(Constant pointer to character)
- This means the pointer cannot be changed (it’s constant), but the data it points to can be modified.
- The pointer is constant, but the data it points to is mutable.
Example:
char str[] = "Hello";
char * const ptr = str;
ptr = "World"; // Error: You cannot change the pointer itself (it's constant).
*ptr = 'A'; // OK: You can modify the content of the data pointed to.
char * const
is useful when you need a pointer to always point to the same memory location, but you want to modify the data at that location.
Summary:
const char *
: The data is constant, but the pointer can change.char * const
: The pointer is constant, but the data can change.
Combined Example:
You can combine both, creating a constant pointer to constant data:
const char * const ptr = "Hello";
ptr = "World"; // Error: You cannot change the pointer.
*ptr = 'A'; // Error: You cannot change the content.
This means neither the pointer nor the data can be changed.
In summary:
const char *
: Pointer to a constant character (modifiable pointer, constant data).char * const
: Constant pointer to a character (constant pointer, modifiable data).
Based on chatGPT
An example of how these two data types work in the aspect of C complier
// Online C compiler to run C program online
#include <stdio.h>
int main() {
const char *pc;
char const* ppc;
char *const pppc;
const char *const pppc;
char c = pc;
char cc = ppc;
char ccc = pppc;
pc = NULL;
ppc = NULL;
pppc = NULL;
pppc = NULL;
return 0;
}
/*
ERROR!
/tmp/gFNT6SlhJE.c: In function 'main':
/tmp/gFNT6SlhJE.c:11:23: error: conflicting types for 'pppc'; have 'const char * const'
11 | const char *const pppc;
| ^~~~
ERROR!
/tmp/gFNT6SlhJE.c:9:17: note: previous declaration of 'pppc' with type 'char * const'
9 | char *const pppc;
| ^~~~
/tmp/gFNT6SlhJE.c:13:14: warning: initialization of 'char' from 'const char *' makes integer from pointer without a cast [-Wint-conversion]
13 | char c = pc;
| ^~
/tmp/gFNT6SlhJE.c:14:15: warning: initialization of 'char' from 'const char *' makes integer from pointer without a cast [-Wint-conversion]
14 | char cc = ppc;
| ^~~
/tmp/gFNT6SlhJE.c:15:16: warning: initialization of 'char' from 'const char *' makes integer from pointer without a cast [-Wint-conversion]
15 | char ccc = pppc;
| ^~~~
/tmp/gFNT6SlhJE.c:19:10: error: assignment of read-only variable 'pppc'
19 | pppc = NULL;
| ^
/tmp/gFNT6SlhJE.c:20:10: error: assignment of read-only variable 'pppc'
20 | pppc = NULL;
| ^
=== Code Exited With Errors ===
*/