博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言中的const和指针
阅读量:6941 次
发布时间:2019-06-27

本文共 2972 字,大约阅读时间需要 9 分钟。

hot3.png

代码

void TC_const(void){    char *a;    char * const   p1        = "char * const   p1;        //const     pointer, non-const data";    char   const * p2        = "char   const * p2;        //non-const pointer,     const data";    const   char * p22       = "const  char  * p22;       //non-const pointer,     const data";    char   const * const p3  = "char   const * const p3;  //const pointer,         const data";    const  char  * const p33 = "const  char  * const p33; //const pointer,         const data";    printf("%s\n", p1);    printf("%s\n", p2);    printf("%s\n", p3);    p1[0]  = 1;   // ok    p1     = a;  //  error: assignment of read-only variable 'p1'    p2[0]  = 2;   // error: assignment of read-only location '*p2'    p22[0] = 2;   // error: assignment of read-only location '*p22'    p2     = a;   // ok;    p22    = a;   // ok;    p3[0]  = 3;   // error: assignment of read-only location '*p3'    p33[0] = 3;   // error: assignment of read-only location '*p33'    p3     = a;   // error: assignment of read-only variable 'p3'    p33    = a;   // error: assignment of read-only variable 'p33'}

 

 

Code::Blocks 17.12 编译提示

mingw32-gcc.exe -Wall -g  -c X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c -o obj\Debug\main.omingw32-g++.exe  -o bin\Debug\vprintf.exe obj\Debug\main.o   X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c: In function 'TC_const':X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:30:9: error: assignment of read-only variable 'p1'  p1     = a;  //  error: assignment of read-only variable         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:33:9: error: assignment of read-only location '*p2'  p2[0]  = 2;   //  error: assignment of read-only variable         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:34:9: error: assignment of read-only location '*p22'  p22[0] = 2;   //  error: assignment of read-only variable         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:38:9: error: assignment of read-only location '*p3'  p3[0]  = 3;   //  error: assignment of read-only variable         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:39:9: error: assignment of read-only location '*p33'  p33[0] = 3;   //  error: assignment of read-only variable         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:40:9: error: assignment of read-only variable 'p3'  p3     = a;  // error: assignment of read-only variable 'p3'|         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c:41:9: error: assignment of read-only variable 'p33'  p33    = a;  // error: assignment of read-only variable 'p3'|         ^X:\sz\2018\c-exp@CodeBlockIDE\ex1-vprintf\vprintf\main.c: In function 'colorAtLinux':

 

总结

 

以*为基准,

  1. const在*的右边出现时,表示指针变量本身是一个常量,可以叫指针常量,其值(指向的地址)不可改变,但指向地址的内容可以改变。
  2. const在*的左边出现时,表示指针变量本身是一个变量,可以叫指针变量,其值(指向的地址)可以改变,但指向地址的内容不可改变。
  3. 两边都出现时,则其值(指向的地址)不可改变,且指向地址的内容也不可改变。

环境

WINDOWS2016

Code::Block 17.12

转载于:https://my.oschina.net/SamXIAO/blog/2992698

你可能感兴趣的文章
数据库----性能优化
查看>>
linux 系统 sar 命令详解
查看>>
QT开发(六十四)——QT样式表(三)
查看>>
LVM卷管理及配额设置
查看>>
嵌入式系统烧写uboot/bootloader/kernel的一般方法
查看>>
RHEL7 配置http虚拟主机
查看>>
Xshell连接Linux下Oracle无法回退的解决办法
查看>>
将字符串倒序输出
查看>>
Web开发:我希望得到的编程学习路线图(转)
查看>>
BZOJ3322[Scoi2013]摩托车交易——最大生成树+贪心+倍增
查看>>
Hive 读取的Column值为NULL?!
查看>>
Java基础学习总结(5)——多态
查看>>
BlackEnergy 针对思科路由器增加了新的***功能
查看>>
Ubuntu下MySQL5.7配置字符集utf8和sql_mode
查看>>
最近几天玩lxc的经历
查看>>
EXCEL2010粘贴复制技巧
查看>>
Network security CA Server
查看>>
ASP.NET MVC Model元数据(一)
查看>>
GIT分支管理是一门艺术
查看>>
android listview item点击后,显示点击效果
查看>>