Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-09-08 19:25:03
forward
601 people have browsed it

C中const char*p、char*const p和const char*const p之间的差异

Pointer

In the C programming language, *p represents the value stored in the pointer, and p represents the address of the value, which is called a pointer.

const char* and char const* indicate that the pointer can point to a constant character, and the value of the character pointed to by the pointer cannot be changed. But we can change the value of the pointer because it is not a constant and can point to another constant character.

char* const means that the pointer can point to a character, and the value of the character pointed to by the pointer can be changed. But we cannot change the value of the pointer because it is now a constant and cannot point to another character.

const char* const means that the pointer can point to a constant character, and the value of the character pointed to by the pointer cannot be changed. We also cannot change the value of the pointer because it is now a constant and cannot point to another constant character.

The principle of naming syntax is from right to left.

// constant pointer to constant char
const char * const
// constant pointer to char
char * const
// pointer to constant char
const char *
Copy after login

Example (C)

Uncomment the erroneous code and view the error.

Real-time demonstration

#include <stdio.h>
int main() {
   //Example: char const*
   //Note: char const* is same as const char*
   const char p = &#39;A&#39;;
   // 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 &#39;*q&#39;
   //*q = &#39;B&#39;;
   const char r = &#39;C&#39;;
   //q can point to another const char
   q = &r;
   printf("%c</p><p>", *q);
   //Example: char* const
   char u = &#39;D&#39;;
   char * const t = &u;
   //You can change the value
   *t = &#39;E&#39;;
   printf("%c", *t);
   // Invalid asssignment
   // t cannot be changed
   // error: assignment of read-only variable &#39;t&#39;
   //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 &#39;*s&#39;
   // *s = &#39;D&#39;;
   // Invalid asssignment
   // s cannot be changed
   // error: assignment of read-only variable &#39;s&#39;
   // s = &r;
   return 0;
}
Copy after login

Output

C
E
Copy after login

The above is the detailed content of Difference between const char*p, char*const p and const char*const p in C. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!