Silent Lamb
C Language const keyword:
--Features.
variables that use this key modifier cannot be changed .
--pointer constant.
Demo:
const char *P or char const * P
--constant pointer.
Demo:
char * Const P
--constant pointer and pointer constant.
1. Constant pointer refers to a char * const p, p is a constant, p cannot be changed to point.
2. The pointer constant refers to a char const *P, p points to a character constant, and p can change direction. But you can't change *p.
--the memory method for constant pointers and pointer constants is as follows.
the C + + programming language A mnemonic method:
Read a statement from right to left.
char * const CP; (* read into pointer to) CP was a const pointer to Char
const char * p; P is a pointer to const char;
/************************************************************************* > File name:const1.c > Author: Silent Lamb Sheep > Mail: [email protected] > Created time:2014 October 20 Monday 19:29 01 seconds ********************************** /#include <stdio.h> #include <string.h> #define Const_pointer# Define Pointer_constint test (void) {/*** * const: keyword, * variable modified with const, cannot be modified after initialization. * * This key value after use is more prone to question the expression: * One: pointer constant *const char *p; * or a *char const *p; * Two: Constant pointer *char * const p; * Three: A constant pointer is also an expression of a pointer constant. *const char * const *P; * * The C + + programming language A mnemonic method: * read a declaration from right to left. * char * const CP; (* read into pointer to) CP are a const pointer to char *const char * p; P is a pointer to const char; */int a = 10;int b = 20;const int * P0; pointer constant int const * P1; pointer constant int * CONST P2 = &b; A constant pointer. const INT * Const P3 = &a; #ifdef const_pointerp0 = &a; printf ("P0 =%d\n", *p0); P0 = &b;printf ("P0 =%d\n", *p0); #if0 *p0 = 10; P0 is a pointer to a constant character. But P0 is not a constant. P0 points to constants of type int. #endifp1 = &a;printf ("P1 =%d\n", *p1);p 1 = &b;printf ("P1 =%d\n", *p1); #if 0*p1 = 10; The variable will error when changing the constant pointed to by the pointer. #endif #endif #ifdef pointer_const#if 0p2 = &a; P2 is a constant. An error occurred while modifying the P2 point to compile. #endif *p2 = 30; printf ("P2 =%d\n", *p2), #endif #if 0 *p3 = 10; Compilation error. P3 = &b; Compilation is an error. #elseprintf ("P3 =%d\n", *p3); #endifreturn 0;} int main (void) {test (); return 0;}
Reprint please indicate the source
http://blog.csdn.net/shaohuazuo/article/details/40316987
C Language const keyword