C Use of C pointer (6) const

Source: Internet
Author: User

(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


C Use of C pointer (6) const

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.