const修饰指针的4种形式
2016-04-17 23:23
330 查看
const修饰指针的4种形式
#include <stdio.h>
int main(void)
{
/* //const修饰指针的4种形式
int a = 5;
// 第一种
const int *p1;
// p本身不是cosnt的,而p指向的变量是const的
*p1 = 3;
// error: assignment of read-only location ‘*p1’
p1 = &a;
// 编译无错误无警告
// 第二种
int const *p2;
// p本身不是cosnt的,而p指向的变量是const的
*p2 = 5;
// error: assignment of read-only location ‘*p2’
p2 = &a;
// 编译无错误无警告
// 第三种
int * const p3;
// p本身是cosnt的,p指向的变量不是const的
*p3 = 5;
// 编译无错误无警告
p3 = &a;
// error: assignment of read-only variable ‘p3’
// 第四种
const int * const p4;// p本身是cosnt的,p指向的变量也是const的
p4 = &a;
// error: assignment of read-only variable ‘p4’
*p4 = 5;
// error: assignment of read-only location ‘*p4’
*/
/*
//const修饰的变量其实是可以改的(前提是gcc环境下)。
const int a = 5;
//a = 6;
// error: assignment of read-only variable ‘a’
int *p;
p = (int *)&a;
// 这里报警高可以通过强制类型转换来消除
*p = 6;
printf("a = %d.\n", a);
// a = 6,结果证明const类型的变量被改了
*/
}
#include <stdio.h>
int main(void)
{
/* //const修饰指针的4种形式
int a = 5;
// 第一种
const int *p1;
// p本身不是cosnt的,而p指向的变量是const的
*p1 = 3;
// error: assignment of read-only location ‘*p1’
p1 = &a;
// 编译无错误无警告
// 第二种
int const *p2;
// p本身不是cosnt的,而p指向的变量是const的
*p2 = 5;
// error: assignment of read-only location ‘*p2’
p2 = &a;
// 编译无错误无警告
// 第三种
int * const p3;
// p本身是cosnt的,p指向的变量不是const的
*p3 = 5;
// 编译无错误无警告
p3 = &a;
// error: assignment of read-only variable ‘p3’
// 第四种
const int * const p4;// p本身是cosnt的,p指向的变量也是const的
p4 = &a;
// error: assignment of read-only variable ‘p4’
*p4 = 5;
// error: assignment of read-only location ‘*p4’
*/
/*
//const修饰的变量其实是可以改的(前提是gcc环境下)。
const int a = 5;
//a = 6;
// error: assignment of read-only variable ‘a’
int *p;
p = (int *)&a;
// 这里报警高可以通过强制类型转换来消除
*p = 6;
printf("a = %d.\n", a);
// a = 6,结果证明const类型的变量被改了
*/
}
相关文章推荐
- 如何阅读一本书(7)
- C运行库详解
- mac下配置ubuntu虚拟机和jenkins
- Java学习笔记一:基本语法:类和对象的介绍、数组、引用类型
- IPadDemo之QQZone
- mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz 安装
- 【C/PY】关于Integer 类型
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 读【我在硅谷做码农】千万富翁的孤独
- 20144303 《Java程序设计》第二次实验实验报告
- .NET 类型转换is和as
- 机房水题欢乐赛 2016-04-17
- 河南囤蒜大户遇“傻子年” 一夜暴富成千万富翁
- 2016年VR&AR有市场吗?
- 20145326网络安全攻防学习平台 - 基础关
- C语言宏连接##问题
- diy数据库(三)--客户端框架的搭建
- 解决 Windows 10 下 MarkdownPad2 预览无法显示
- LAMP 快速部署
- leetcode_090 Subsets II