当前位置:编程学习 > C/C++ >>

修改const变量的疑问

2个指针指向的地址和变量地址一样,值为什么不一样呢?疑惑。
#include "stdafx.h"
 
 
int _tmain(int argc, _TCHAR* argv[])
{
    const int a=10;
    const int *p1=&a;
    int *p2=const_cast<int *>(p1);
    if (&a==p1)
    {
        printf("&a==p1\n");
    }
    else
    {
        printf("&a!=p1\n");
    }
    if (p2==p1)
    {
        printf("p2==p1\n");
    }
    else
    {
        printf("p2==p1\n");
    }
    printf("%#p:%d %#p:%d %#p:%d\n",&a,a,p1,*p1,p2,*p2);
    *p2=20;
    if (&a==p1)
    {
        printf("&a==p1\n");
    }
    else
    {
        printf("&a!=p1\n");
    }
    if (p2==p1)
    {
        printf("p2==p1\n");
    }
    else
    {
        printf("p2==p1\n");
    }
    printf("%#p:%d %#p:%d %#p:%d\n",&a,a,p1,*p1,p2,*p2);
    return 0;
}
 
\

 作者“Gezidan Studio (鸽子蛋)”
 

补充:软件开发 , C语言 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,