Using the const method to use the c pointer (6) and the const method to use the const Method
(6) Use of const
Const in c expresses the meaning of "constant", which is more accurate to the meaning of "read-only. When a const encounters a pointer, it produces different results due to its different relative positions.
Example
(1) The const is located on the left side *.
For example, const int * p; this is equivalent to int const * p;
In this case, const indicates that * p is read-only.
(1) const is on the right side *
For example, int * const p;
In this case, const indicates that p is read-only.
Lab Verification
Int main () {int foo = 1; int bar = 2; const int * pa = & foo; int * const pb = & foo; // try to modify the value * pa = 2; // The Compiler reports an error: "The expression must be a modifiable left value" pb = & bar; // The Compiler reports an error: "The expression must be a modifiable left value" return 0 ;}
In this case, the add statement * pa = 2; reports an error because * pa is read-only and cannot be changed; pb = & bar; also reports an error because pb is read-only and cannot be changed.
As mentioned above, const is intended to be "read-only" rather than "constant ". You can change its value in some ways.
Int main () {const int foo = 1; printf ("foo... % d \ n ", foo); int * pa = & foo; // There is a warning here, but it can still run * pa = 2; printf (" foo... % d \ n ", foo); system (" pause "); return 0 ;}
Run
The result shows that the read-only foo is successfully modified. This code is compiled in c only with a warning. compilation in cpp directly fails. I don't know if there are any other methods. you can recommend them.
Const meets the struct type. To what extent does const meet?
Typedef struct {char * name; char * address; int age;} Person; void setPerson (const Person * pPer) {/* the following error occurs! Upper-> name = "David"; upper-> address = "BeiJing"; upper-> age = 22; */strcpy (upper-> name, sizeof ("David"), "David"); strcpy (pPer-> address, sizeof ("BeiJing"), "BeiJing");} int main () {Person per; char name [10] = "zx"; char address [20] = "QiChun"; per. name = name; per. address = address; per. age = 24; printf ("per. name... % s \ n ", per. name); printf ("per. address... % s \ n ", per. address); printf ("per. age... % d \ n ", per. age); printf ("update .. \ n "); setPerson (& per); printf (" per. name... % s \ n ", per. name); printf ("per. address... % s \ n ", per. address); printf ("per. age... % d \ n ", per. age); return 0 ;}
Run
There is no warning during compilation. From the running result, the const modifier plays the following role:
If you initialize per = {"zx", "QiChun"} directly, you cannot modify the content pointed to by name and address in the setPerson () method. This is not because of const, but because "zx" and "QiChun" are both constant strings. Generally, constant strings are in the read-only area of the memory and cannot be modified. Therefore, in the code, we choose to apply for stack space.
Column Directory: C pointer
In C language, why use const to define pointers as function parameters?
Habits ......
If you use const, you cannot change it in your function body. Otherwise, an error is returned!
This is to enable some pointers to be called by other functions, but you do not want them to be set due to errors accidentally changed by other functions.
If you want to change the pointer, of course you should not add const.
Const pointer C Language
Const is only responsible for the variables it declares, and only checks during compilation. When you forcibly convert it into a type, you can modify it.
Your compiler is a bit old. The new compiler has an error in int * B = & a; because there is an implicit transformation from const int * to int.
In addition, the pointer to the C language is flexible, but you need to pay special attention to security when using it. a can be changed directly like this:
Const int a = 3;
* (Int *) a = 5;