posts - 137,comments - 0,views - 40818
复制代码
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main() {
    int girl = 19;
    int boy = 21;

    int const* p1 = &girl;  //不能修改指向变量的值,其中int const * p1 = &girl等同于const int * p1 = &girl;
    //*p1 = 22;  //这些有注释的都是不能被正常运行的
    girl = 26;
    //p1 = 28;
    p1 = &boy;
    printf("p1所指向的变量的值是:%d\n", *p1);
    printf("girl的年龄是:%d\n", girl);
    printf("boy的年龄是:%d\n\n", boy);

    int* const p2 = &girl;  //不允许指向别的地址
    *p2 = 27;
    printf("p2所指向的变量的值是:%d\n", *p2);
    girl = 33;
    //p2 = &boy;
    printf("girl的年龄是:%d\n\n", girl);

    const int* const p3 = &girl;  //不允许指向别的地址,也不能修改指向变量的值
    //*p3 = 27;
    girl = 35;
    //p3 = &boy;         
    printf("p2所指向的变量的值是:%d\n", *p3);
    printf("girl的年龄是:%d\n", girl);

    system("pause");
    return 0;
}   //总结:const离谁越近,谁就不能被修改
复制代码

posted on   wshidaboss  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
· Manus的开源复刻OpenManus初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示