nowcoder错题查缺补漏

此博客用来总结牛客网刷题过程中一些错题,特别是一些自己没有搞懂的、模糊的知识点。会尽量按照知识点来分类。

const

NO.1

1
2
3
4
5
6
7
8
9
10
11
12
//以下程序输出是____。
#include <iostream>
using namespace std;
int main(void)
{
const int a = 10;
int * p = (int *)(&a);
*p = 20;
cout<<"a = "<<a<<", *p = "<<*p<<endl;
return 0;
}
//答案:a = 10, *p = 20

在C++中,a作用域下在C++中所有的常量a在预编译阶段就已经做了类似于宏替换的工作,所以输出的是它的常量值,尽管a所对应的内存地址上的值已经发生改变,但还是输出常量值。

本文标题:nowcoder错题查缺补漏

文章作者:微石

发布时间:2018年06月04日 - 09:06

最后更新:2018年07月24日 - 09:07

原始链接:akihoo.github.io/posts/585b7684.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。