Const usage-read only

Source: Internet
Author: User

The const usage is detailed

Const READONLY (Read only)

Example one: read-only int variable
int main (int argc,const char * argv[]) {const INT A1;    int const A2;    A1 = 2;//A2 = 3;    printf ("%d\n", A1);   printf ("%d\n", A2); }

The above example, when declaring variables A1 and A2 of type int, is decorated with the Const modifier, which means that the variables A1 and A2 are read-only int type variables, which are initialized by default and cannot be modified and assigned again.

Output Print Results

0

0

Program ended with exit code:0


Example two: read-only type int variable/int variable memory address not read-only

memory address = pointer

Int main (int argc,const char * argv[]) {    int a=10;     int b=12;        int const *a1=&a ;    const int *a2=&a;    int const *a3=null;             printf ("%p\n", A1);     printf ("%p\n", a2);     printf ("%p\n", A3);     printf ("%d\n", *A1);     printf ("%d\n", *A2);//    printf ("%d\n", *a3);     //    *a1 = 11; //pointer a1 The int value pointed to read only//     *a2 = 12; //pointer a2 The int value pointed to read only    a1 = &b;     a2 = &b;    printf ("%p\n", A1);     printf ("%p \ n ", A2);    printf ("%p\n", A3);     printf ("%d\n", *A1);     printf ("%d\n" , *A2);//    printf ("%d\n", *A3);}

The above example declares the int pointer A1 and A2, while using the const modifier, here to figure out who the const modifier is, where the const modifier is the int type, so the value of the int type pointed to by the pointer cannot be modified and assigned again. The Pointers (memory addresses) A1 and A2 can be modified and assigned again. When you modify the value (memory address) of a pointer, the value that the pointer points to also changes.

Output printing,

0x7fff5fbff7ac

0x7fff5fbff7ac

0x0

10

10

0x7fff5fbff7a8

0x7fff5fbff7a8

0x0

12

12

Program ended with exit code:0


Example three: Memory address read-only/memory address store value is not read-only
int main (int argc,const char * argv[]) {int a = 12;    int * Const A1 = &a;    printf ("%p\n", A1);       printf ("%d\n", *A1); int b = 14;//A1 = &b;    Memory address A1 Read only, cannot be modified again and assigned value *A1 = b;    printf ("%p\n", A1); printf ("%d\n", *A1);}

As the above example, const modifies the int*, which is the int pointer, which is the memory address is const, and the memory address store value is not const, that is, non-read only.

Output printing,

0x7fff5fbff7ac

12

0x7fff5fbff7ac

14

Program ended with exit code:0


Example four: read-only int variable/read-only memory address
int main (int argc,const char * argv[]) {int a = 12;    const INT * Const B = &a;    printf ("%p\n", b);        printf ("%d\n", *b);    int a1 = 13;//b=&a1;//*b=a1;    printf ("%p\n", b); printf ("%d\n", *b);}

As the above example, or to understand the Const modified who, here there are two const, the previous const modified is an int, that is, the memory address stored value is read-only. The next const modifier is *, which is the pointer, meaning that the memory address is read-only.

The output is printed as follows

0x7fff5fbff7ac

12

0x7fff5fbff7ac

12

Program ended with exit code:0


Identify the const modifier who

The const is behind who can not be modified, the const in the front will be moved one after another, the two equivalent

Const effect

(1) To prevent a variable from being changed, you can use the Const keyword. When defining the const variable, it is often necessary to initialize it, since there is no chance to change it again;

(2) For pointers, you can specify that the pointer itself is a const, or you can specify that the pointer refers to a const data, or both of which are specified as const;

(3) In a function declaration, the const can modify the formal parameter, indicating that it is an input parameter, the value cannot be changed inside the function;

(4) For a class member function, if it is specified as a const type, it indicates that it is a constant function and cannot modify the member variables of the class;

======end======

Const usage-read only

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.