相关疑难解决方法(0)

同一地址的变量如何产生2个不同的值?

考虑一下:

#include <iostream>
using namespace std;

int main(void)
{
    const int a1 = 40;
    const int* b1 = &a1;
    char* c1 = (char *)(b1);
    *c1 = 'A';
    int *t = (int*)c1;


    cout << a1 << " " << *t << endl;
    cout << &a1 << " " << t << endl; 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个输出是:

40 65 
0xbfacbe8c 0xbfacbe8c
Run Code Online (Sandbox Code Playgroud)

除非编译器进行优化,否则这对我来说几乎是不可能的.怎么样 ?

c++ casting const undefined-behavior

6
推荐指数
1
解决办法
533
查看次数

标签 统计

c++ ×1

casting ×1

const ×1

undefined-behavior ×1