C++

第一眼见到explicit和volatile可能会一愣一愣的觉得可能是c11或者c14新加的标识符。

其实不是这样,volatile和const两个关键字在C语言的第二个版本KR C的时候就被加入了C标准,他们是两个相对的关键字

1const 修饰符表示这是一个常量类型,这个变量的值不会被程序改变 2 3volatile 修饰符表示这个变量可能被编译器以外的行为(譬如内联汇编程序)改变。

修饰常量变量只要和类型紧挨着就可以

1int const a = 1; 2const int a = 1;

修饰指针时以*号为分界符号

1#include <iostream> 2#include <iostream> 3 4int main() { 5 int a = 10; 6 int b = 20; 7 8 const int *p1 = &a; 9 int const *p2 = &a; 10 int *const p3 = &a; 11 const int *const p4 = &a; 12 int const *const p5 = &a; 13 14 printf("&a = %X\n", &a); 15 printf("&b = %X\n", &b); 16 17 printf("p1 = 0x%X , *p1 = %d\n", p1, *p1); 18 printf("const int *p1 = &a; \n (*p1) = b;通过p修改a的值\n"); 19// (*p1) = b; 20 p1 = &b; 21 printf("p1 = 0x%X , *p1 = %d\n", p1, *p1); 22 printf("p2 = 0x%X , *p2 = %d\n", p2, *p2); 23 printf("int const *p2 = &a; \n (*p2) = b;通过p修改a的值\n"); 24// (*p2) = b; 25 p2 = &b; 26 printf("p2 = 0x%X , *p2 = %d\n", p2, *p2); 27 printf("p3 = 0x%X , *p3 = %d\n", p3, *p3); 28 printf("int *const p3 = &a; \n p3 = &b;修改p的指向\n"); 29// p3 = &b; 30 (*p3) = b; 31 printf("p3 = 0x%X , *p3 = %d\n", p3, *p3); 32 printf("p4 = 0x%X , *p4 = %d\n", p4, *p4); 33 printf("const int *const p4 = &a; \n 二者都不能修改\n"); 34// (*p4) = &b; 35// p4 = &b; 36 printf("p4 = 0x%X , *p4 = %d\n", p4, *p4); 37 printf("p5 = 0x%X , *p5 = %d\n", p5, *p5); 38 printf("int const *const p5 = &a; \n 二者都不能修改\n"); 39// (*p5) = &b; 40// p5 = &b; 41 printf("p5 = 0x%X , *p5 = %d\n", p5, *p5); 42 return 0; 43}

gcc报错结果

1J:\SITP\alg\main.cpp: In function 'int main()': 2J:\SITP\alg\main.cpp:14:11: error: assignment of read-only location '* p1' 3 (*p1) = b; 4 ^ 5J:\SITP\alg\main.cpp:17:11: error: assignment of read-only location '* p2' 6 (*p2) = b; 7 ^ 8J:\SITP\alg\main.cpp:20:8: error: assignment of read-only variable 'p3' 9 p3 = &b; 10 ^ 11J:\SITP\alg\main.cpp:23:11: error: assignment of read-only location '*(const int*)p4' 12 (*p4) = &b; 13 ^ 14J:\SITP\alg\main.cpp:23:11: error: invalid conversion from 'int*' to 'int' [-fpermissive] 15J:\SITP\alg\main.cpp:24:8: error: assignment of read-only variable 'p4' 16 p4 = &b; 17 ^ 18J:\SITP\alg\main.cpp:26:11: error: assignment of read-only location '*(const int*)p5' 19 (*p5) = &b; 20 ^ 21J:\SITP\alg\main.cpp:26:11: error: invalid conversion from 'int*' to 'int' [-fpermissive] 22J:\SITP\alg\main.cpp:27:8: error: assignment of read-only variable 'p5' 23 p5 = &b; 24 ^ 25mingw32-make.exe[3]: *** [CMakeFiles/alg.dir/main.cpp.obj] Error 1 26mingw32-make.exe[2]: *** [CMakeFiles/alg.dir/all] Error 2 27mingw32-make.exe[1]: *** [CMakeFiles/alg.dir/rule] Error 2 28CMakeFiles\alg.dir\build.make:61: recipe for target 'CMakeFiles/alg.dir/main.cpp.obj' failed 29CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/alg.dir/all' failed 30CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/alg.dir/rule' failed 31mingw32-make.exe: *** [alg] Error 2 32Makefile:117: recipe for target 'alg' failed

将非法的行注释掉之后可以看到允许修改的内容

1J:\SITP\alg\cmake-build-debug\alg.exe 2&a = 72FE24 3&b = 72FE20 4p1 = 0x72FE24 , *p1 = 10 5const int *p1 = &a; 6 (*p1) = b;通过p修改a的值 7p1 = 0x72FE20 , *p1 = 20 8p2 = 0x72FE24 , *p2 = 10 9int const *p2 = &a; 10 (*p2) = b;通过p修改a的值 11p2 = 0x72FE20 , *p2 = 20 12p3 = 0x72FE24 , *p3 = 10 13int *const p3 = &a; 14 p3 = &b;修改p的指向 15p3 = 0x72FE24 , *p3 = 20 16p4 = 0x72FE24 , *p4 = 20 17const int *const p4 = &a; 18 二者都不能修改 19p4 = 0x72FE24 , *p4 = 20 20p5 = 0x72FE24 , *p5 = 20 21int const *const p5 = &a; 22 二者都不能修改 23p5 = 0x72FE24 , *p5 = 20 24 25Process finished with exit code 0
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Volatile概述

Volatile概念volatile是一个特征修饰符(typespecifier)。volatile的作用是作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值。volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。——百度百科所以呢它主要是两个作用:一个是

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid