The content of C + +-explicit and volatile/const that is particularly promising

Source: Internet
Author: User
Tags volatile

The first eye to see explicit and volatile may be one Leng Leng thought may be C11 or C14 new identifier.

In fact, the volatile and const two keywords were added to the C standard when the second version of the C language was KR C, and they were two relative keywords

Const modifier means that this is a constant type, and the value of the variable is not changed by the program The volatile modifier indicates that the variable may be changed by behavior other than the compiler (for example, inline assembler).

Modify constant variables as long as they are next to the type

int Const 1 ; Const int 1;

To modify the pointer with an * number as the demarcation symbol

#include <iostream>#include<iostream>intMain () {intA =Ten; intb = -; Const int*P1 = &A; int Const*P2 = &A; int*ConstP3 = &A; Const int*ConstP4 = &A; int Const*ConstP5 = &A; printf ("&a =%x\n", &a); printf ("&b =%x\n", &b); printf ("P1 = 0x%X, *p1 =%d\n", p1, *p1); printf ("const int *P1 = &a; \ n (*p1) = B; Modify the value of a by P \ n");//(*P1) = b;P1 = &b; printf ("P1 = 0x%X, *p1 =%d\n", p1, *p1); printf ("P2 = 0x%X, *p2 =%d\n", p2, *p2); printf ("int const *P2 = &a; \ n (*p2) = B; Modify the value of a by P \ n");//(*P2) = b;P2 = &b; printf ("P2 = 0x%X, *p2 =%d\n", p2, *p2); printf ("p3 = 0x%X, *p3 =%d\n", P3, *p3); printf ("int *const p3 = &a; \ n P3 = &b; Modify P's point \ n");//P3 = &b;(*P3) =b; printf ("p3 = 0x%X, *p3 =%d\n", P3, *p3); printf ("P4 = 0x%X, *p4 =%d\n", P4, *p4); printf ("const int *const P4 = &a; \ n Neither can be modified \ n");//(*P4) = &b;//P4 = &b;printf"P4 = 0x%X, *p4 =%d\n", P4, *p4); printf ("P5 = 0x%X, *p5 =%d\n", P5, *p5); printf ("int const *const P5 = &a; \ n Neither can be modified \ n");//(*P5) = &b;//P5 = &b;printf"P5 = 0x%X, *p5 =%d\n", P5, *p5); return 0;}

GCC error results

J:\sitp\alg\main.cpp:in function'int main ()': J:\SITP\alg\main.cpp: -: One: Error:assignment of Read-only location'* P1'     (*P1) =b; ^J:\SITP\alg\main.cpp: -: One: Error:assignment of Read-only location'* P2'     (*P2) =b; ^J:\SITP\alg\main.cpp: -:8: Error:assignment of read-only variable'P3'P3= &b; ^J:\SITP\alg\main.cpp: at: One: Error:assignment of Read-only location'* (const int*) P4'     (*P4) = &b; ^J:\SITP\alg\main.cpp: at: One: Error:invalid Conversion from 'int*'To'int'[-Fpermissive] J:\SITP\alg\main.cpp: -:8: Error:assignment of read-only variable'P4'P4= &b; ^J:\SITP\alg\main.cpp: -: One: Error:assignment of Read-only location'* (const int*) P5'     (*P5) = &b; ^J:\SITP\alg\main.cpp: -: One: Error:invalid Conversion from 'int*'To'int'[-Fpermissive] J:\SITP\alg\main.cpp: -:8: Error:assignment of read-only variable'P5'P5= &b; ^Mingw32-make.exe[3]: * * * [cmakefiles/alg.dir/main.cpp.obj] Error1Mingw32-make.exe[2]: * * * [Cmakefiles/alg.dir/all] Error2Mingw32-make.exe[1]: * * * [cmakefiles/alg.dir/rule] Error2Cmakefiles\alg.dir\build.make: A: recipe forTarget'Cmakefiles/alg.dir/main.cpp.obj'Failedcmakefiles\makefile2: the: recipe forTarget'Cmakefiles/alg.dir/all'Failedcmakefiles\makefile2: +: recipe forTarget'Cmakefiles/alg.dir/rule'Failedmingw32-make.exe: * * * [ALG] Error2Makefile:117: recipe forTarget'ALG'Failed

You can see what is allowed to be modified after you comment out an illegal line

J:\SITP\alg\cmake-build-Debug\alg.exe&a =72fe24&b =72fe20p1=0x72fe24, *P1 =TenConst int*P1 = &A; (*P1) =b; Modify the value of a by P P1=0x72fe20, *P1 = -P2=0x72fe24, *P2 =Tenint Const*P2 = &A; (*P2) =b; Modify the value of a by P P2=0x72fe20, *P2 = -P3=0x72fe24, *P3 =Tenint*ConstP3 = &A; P3= &b; Change the point of P P3=0x72fe24, *P3 = -P4=0x72fe24, *P4 = -Const int*ConstP4 = &A; neither can modify P4=0x72fe24, *P4 = -P5=0x72fe24, *P5 = -int Const*ConstP5 = &A; neither can modify P5=0x72fe24, *P5 = -Process finished with exit code0

The content of C + +-explicit and volatile/const that is particularly promising

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.